This article lists several methods:
1. Use java.math.BigDecimal
2. Use java.text.DecimalFormat
3. Use java.text.NumberFormat
4. Use java.util.Formatter
5. Use String.format
At the end of the article, I shared more expansion knowledge with you. In addition, you can implement it yourself or borrow the encapsulated class library to implement it. I will not list them one by one in this article. Let’s take a look at the detailed introduction below.
1. Use BigDecimal and keep the 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 retain the 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 retain the two decimal places
public static String format3(double value) { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); /* * setMinimumFractionDigits to 2 * * If you do not do this, then when the value of value is 100.00, return 100 * * instead of 100.00 */ nf.setMinimumFractionDigits(2); nf.setRoundingMode(RoundingMode.HALF_UP); /* * If the format you want to output is separated by commas, you can set it to true */ nf.setGroupingUsed(false); return nf.format(value);}4. Use java.util.Formatter and keep the two decimal places
public static String format4(double value) { /* * %.2f % means any digit before the decimal point 2. The result after two decimal format is f. The floating point type is */ return new Formatter().format("%.2f", value).toString();}5. Use String.format to implement it.
public static String format5(double value) { return String.format("%.2f", value).toString();}Extended knowledge
As a text processing tool, String.format provides us with powerful and rich string formatting functions.
Format floating point numbers
The placeholder format is: %[index$][identification]*[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.457Available IDs:
-, left-aligned within the minimum width, cannot be used with the 0 mark.
0. If the content length is less than the minimum width, fill it with 0 on the left.
#, add a 0 before ength and hexadecimal, add 0x before hexadecimal.
+, the result always contains a + or - sign.
Space, add space before positive numbers, and add - sign before negative numbers.
,, only used with decimal, separated by every 3 digits.
(, If the result is a negative number, wrap it in brackets and no symbols are displayed.
Available converters:
b, Boolean type, as long as the actual parameter is a Boolean type that is not false, it is formatted as a string true, otherwise it is a string false.
n, a platform-independent newline character, can also be obtained through System.getProperty("line.separator").
f, floating point type (decimal). Displays 9 significant numbers and will be rounded. Such as 99.99.
a, floating point number type (hexadecimal).
e, exponential type. Such as 9.38e+5.
g, floating point number type (shorter than %f, %a, displays 6-digit valid numbers, and will be rounded)
Summarize
The above is the entire content of the multiple writing methods of two decimal places retained in Java. I hope that the content of this article will be of certain help to everyone's study or work. If you have any questions, you can leave a message to communicate.