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

This paper lists several methods: @ h_ 419_ 5@

1. Use Java math. BigDecimal @H_ 419_ 5@

2. Use Java text. DecimalFormat @H_ 419_ 5@

3. Use Java text. NumberFormat @H_ 419_ 5@

4. Use Java util. Formatter @H_ 419_ 5@

5. Use string format @H_ 419_ 5@

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@ H_ 419_ 5@

1、 Use BigDecimal and keep two digits @ h after the decimal point_ 419_ 5@

2、 Use decimalformat and keep two digits @ h after the decimal point_ 419_ 5@

3、 Use numberformat and keep two digits @ h after the decimal point_ 419_ 5@

4、 Using Java util. Formatter, two decimal places @ h_ 419_ 5@

5、 Use string Format@ H_ 419_ 5@

Extended knowledge @ h_ 419_ 5@

String. As a text processing tool, format provides us with powerful and rich string formatting functions@ H_ 419_ 5@

Format floating point number @ h_ 419_ 5@

Placeholder format:% [index $] [ID] * [minimum width] [. Precision] converter @ h_ 419_ 5@

Available ID: @ h_ 419_ 5@

-, align left within the minimum width and cannot be used with the 0 identification@ H_ 419_ 5@

0. If the content length is less than the minimum width, fill it with 0 on the left@ H_ 419_ 5@

#, for octal and hexadecimal, add a 0 before octal, and add 0x before hexadecimal@ H_ 419_ 5@

+, the result always contains a + or - sign@ H_ 419_ 5@

Spaces, plus spaces before positive numbers, plus minus signs before negative numbers@ H_ 419_ 5@

, only with decimal, every 3 digits are separated by@ H_ 419_ 5@

(, if the result is negative, it is enclosed in parentheses and the symbol is not displayed. @ h)u 419)u 5@

Available Converters: @ h_ 419_ 5@

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@ H_ 419_ 5@

n. Platform independent line breaks can also be used through system Get property ("line. Separator")@ H_ 419_ 5@

f. Floating point type (decimal). 9 significant digits are displayed and rounded. For example, 99.99. @ h)u 419)u 5@

a. Floating point type (hexadecimal). @h)u 5@

e. Index type. Such as 9.38e + 5@ H_ 419_ 5@

g. Floating point type (shorter than the length of% F,% a, displaying 6 significant digits and rounding) @h)u 419)u 5@

Summary @ h_ 419_ 5@

The above is all the content of multiple ways to write two decimal places in Java. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message@ H_ 419_ 5@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>