Java is a summary of several ways of writing with two decimal places

  • 2020-05-17 05:37:48
  • OfStack

This article lists several methods:

1. Use java.math.BigDecimal

2. Use java.text.DecimalFormat

Use 3. java.text.NumberFormat

Use 4. java.util.Formatter

5. Use String.format

At the end of the article to share more knowledge, in addition to their own implementation or use the encapsulated library to achieve, in this article will not list 11. Let's take a look at the details.

1. Use BigDecimal, leaving two decimal places behind


public static String format1(double value) {

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

2. Use DecimalFormat, leaving two decimal places behind


public static String format2(double value) {

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

3. Use NumberFormat, leaving two decimal places behind


public static String format3(double value) {

 NumberFormat nf = NumberFormat.getNumberInstance();
 nf.setMaximumFractionDigits(2);
 /*
  * setMinimumFractionDigits Set to 2
  * 
  *  If not, then when value The value is 100.00 Time to return 100
  * 
  *  Rather than 100.00
  */
 nf.setMinimumFractionDigits(2);
 nf.setRoundingMode(RoundingMode.HALF_UP);
 /*
  *  If you want the output format to be separated by commas, you can set to true
  */
 nf.setGroupingUsed(false);
 return nf.format(value);
}

4. Use java.util.Formatter, two decimal places to the right


public static String format4(double value) {
 /*
  * %.2f %  said   Any number before the decimal point  2  Two decimal places   The result after formatting is  f  Represent floating point type 
  */
 return new Formatter().format("%.2f", value).toString();
}

5. Use String.format.


public static String format5(double value) {

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

Expand the knowledge

String.format As a text processing tool, we provide a powerful and rich string formatting capabilities.

Formatting floating point Numbers

The placeholder format is: %[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:

-, left aligned within the minimum width, cannot be used from 0 to 1.

0, if the length of the content is less than the minimum width, fill the left side with 0.

For hexadecimal and hexadecimal, add 1 before hexadecimal, and 0x before hexadecimal.

Plus, the result always contains a plus or minus sign.

Spaces, Spaces before positive Numbers, minus signs before negative Numbers.

Only be separated from every three digits in base 10.

(, if the result is negative, it is enclosed in parentheses and no sign is shown.

Available converters:

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

n, a platform-independent newline, can also be obtained from System.getProperty (" line.separator ").

f, floating point (base 10). Displays 9 significant digits and rounds 4 and 5. Such as 99.99.

a, floating point (base 106).

e, type of exponent. Such as 9.38 e + 5.

g, floating point (shorter than %f, %a, shows 6 significant digits, rounds 4 and 5)

conclusion

The above is all the content of Java which retains two decimal points and various writing methods. I hope the content of this paper can bring you some help in your study or work. If you have any questions, you can leave a message to communicate.


Related articles: