# Summary of several writing methods of retaining two decimal places in Java

Keywords: Java Back-end

I believe you may have such a business requirement when you are working on a project: the data displayed on the page or interface shall be kept to two decimal places. Then, this article will share with you several ways of using Java to retain two decimal places. The article gives a detailed example code, which is very helpful for your learning and understanding. Friends in need, let's learn together.

# This paper lists several methods:

1. Use java.math.BigDecimal

2. Use java.text.DecimalFormat

3. Use java.text.NumberFormat

4. Using java.util.Formatter

5. Use String.format

At the end of the article, I share more expanded knowledge. In addition, I can implement it myself or borrow the encapsulated class library. I won't list them one by one in this article. Let's take a look at the detailed introduction.

# 1, Use BigDecimal and keep two decimal places

```public static String format1(double value) {

BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.HALF_UP);
return bd.toString();
}
```

# 2, Use DecimalFormat to keep two decimal places

```public static String format2(double value) {

DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(value);
}
```

# 3, Use NumberFormat to keep two decimal places

```public static String format3(double value) {

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
/*
* setMinimumFractionDigits Set to 2
*
* If you don't, 100 is returned when the value of value is 100.00
*
*/
nf.setMinimumFractionDigits(2);
nf.setRoundingMode(RoundingMode.HALF_UP);
/*
* If you want the output format separated by commas, you can set it to true
*/
nf.setGroupingUsed(false);
return nf.format(value);
}
```

# 4, Use java.util.Formatter to keep two decimal places

```public static String format4(double value) {
/*
* %.2f % Represents any number of digits before the decimal point, 2 represents two decimal places, and the result after the format is f represents floating point type
*/
return new Formatter().format("%.2f", value).toString();
}
```

# 5, Use String.format to implement

```public static String format5(double value) {

return String.format("%.2f", value).toString();
}
```

# Expand knowledge

As a text processing tool, String.format provides us with powerful and rich string formatting functions.

Format floating point numbers

Placeholder format:% [index \$] [ID] * [minimum width] [. Precision] converter

```double num = 123.4567899;
System.out.print(String.format("%f %n", num)); // 123.456790
System.out.print(String.format("%a %n", num)); // 0x1.edd3c0bb46929p6
System.out.print(String.format("%g %n", num)); // 123.457
```

Available identification:

-, align left within the minimum width and cannot be used with the 0 identification.

0. If the content length is less than the minimum width, fill it with 0 on the left.

+, the result always contains a + or - sign.

Spaces, plus spaces before positive numbers, plus minus signs before negative numbers.

, only with decimal, every 3 digits are separated by.

(, if the result is negative, it is enclosed in parentheses and no symbol is displayed.

Available Converters:

b. Boolean type. As long as the argument is a boolean type other than false, it is formatted as string true, otherwise it is string false.

n. Platform independent line breaks can also be obtained through System.getProperty("line.separator").

f. Floating point type (decimal). 9 significant digits are displayed and rounded. For example, 99.99.