This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
Hello, hello, I am gray little ape, a super bug writing program ape!
Today I encountered a small problem in the development process about how to keep data of type double as two decimal places. Suddenly found this aspect has a little lack, to sum up.
The String class
This method is implemented using the format() method of String, which is used to format data. The first argument is passed in a String to indicate the format of the output data, using “%.2f” if two decimal places are reserved. The second argument is the data to be formatted. Examples are as follows:
Double testDounle_01 = 123.456; Float testFloat_01 = 456.125 f; /** / system.out.println (" string "); /** / system.out.println (" string "); System.out.println(String.format("%.2f", testDounle_01)); System.out.println(String.format("%.2f", testFloat_01));Copy the code
Second, DecimalFormat classes
The DecimalFormat class is primarily used to format decimal data, and is a concrete subclass of NumberFormat that is easy to manipulate and use. When used, the class needs to be instantiated, and the format type of the data passed in the constructor.
The meanings of each symbol are as follows:
The use cases are as follows:
Public static void testDoubleToTwo() {/** *DecimalFormat class */ DecimalFormat DecimalFormat = new DecimalFormat("#.00"); String ans_3 = decimalFormat.format(testDounle_01); System.out.println(ans_3); }Copy the code
BigDecimal class for data processing
The API class BigDecimal, provided in the java.Math package, is used to perform precise operations on numbers that exceed 16 significant bits. The floating-point variable double can handle 16-bit significant numbers. However, it can also be used to define two decimal places after the decimal point, but it is cumbersome to use and is not recommended.
Double testDounle_01 = 123.456; Float testFloat_01 = 456.125 f; /** * BigDecimal BigDecimal = new BigDecimal(testDounle_01); double ans_2 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(ans_2);Copy the code
The NumberFormat class performs data processing
The NumberFormat class is a data formatting class that uses it to keep floating point numbers in binary in a similar way to BigDecimal, but is also cumbersome to use.
Double testDounle_01 = 123.456; Float testFloat_01 = 456.125 f; / * * * NumberFormat class data processing * * / NumberFormat numberInstance = NumberFormat. GetNumberInstance (); / / set the maximum decimal digits numberInstance setMaximumFractionDigits (2); / / set data rounding type numberInstance. SetRoundingMode (RoundingMode. HALF_UP); System.out.println(numberInstance.format(testDounle_01));Copy the code
To summarize
I prefer the first two methods for saving floating-point data to two decimal places, or simply using the string.format () method,
Friends have other methods welcome to put forward corrections!
I’m Grey Ape, and I’ll see you next time!