In the actual project development, there will always be floating-point number rounding to retain a few decimal points, so the collection of several common methods:
Directly on the code (keep two decimal places).
Format.java:
ImportJava.math.BigDecimal;ImportJava.text.DecimalFormat;ImportJava.text.NumberFormat;classFormat {Private DoubleSrc_num; PublicFormat (Doublenum) {Src_num=num; } /** BigDecimal, Digital construction*/ Public voidfun1 () {BigDecimal bg=NewBigDecimal (Src_num); //Number 2 stands for two-bit retention DoubleDes_num = Bg.setscale (2, bigdecimal.round_half_up). Doublevalue (); System.out.println (Des_num); } /** BigDecimal, String construction*/ Public voidfun2 () {BigDecimal bg=NewBigDecimal (string.valueof (src_num)); //Number 2 stands for two-bit retention DoubleDes_num = Bg.setscale (2, bigdecimal.round_half_up). Doublevalue (); System.out.println (Des_num); } /** DecimalFormat*/ Public voidFun3 () {//#.00 represents two decimal places, #.0000 four decimal places, etc...DecimalFormat DF =NewDecimalFormat ("#.00"); System.out.println (Df.format (src_num)); } /** String.Format*/ Public voidFun4 () {//%. Represents any number of digits before the decimal point, 2 means two decimal places, and the formatted result is f for floating point typeSystem.out.println (String.Format ("%.2f", Src_num)); } /** NumberFormat*/ Public voidfun5 () {NumberFormat NF=numberformat.getnumberinstance (); //2 Sets the maximum number of digits to display for the formatted object after the decimal point, the last bit shown is roundedNf.setmaximumfractiondigits (2); System.out.println (Nf.format (src_num)); } /** Math.Round*/ Public voidfun6 () {DoubleDes_num = (Double) Math.Round (src_num * 100)/100; System.out.println (Des_num); }}
Testdemo.java:
Public classTestdemo { Public Static voidMain (string[] args) {Doublenum = 3.1415926; //double num = 4.015; //double num = 4.016; //double num = 999999999.015;Format format =NewFormat (num); Format.fun1 (); Format.fun2 (); Format.fun3 (); Format.fun4 (); Format.fun5 (); Format.fun6 (); }}
Let's look at the results of the operation:
3.1415926
3.143.143.143.143.143.14
4.015:
4.014.024.014.024.014.01
4.016:
4.024.024.024.024.024.02
999999999.015:
9.9999999901e89.9999999902e8999999999.01999999999.02999,999,999.019.9999999902e8
General development, the accuracy of the requirements of the calculation is not too strict, the above methods are applicable (scientific counting method will be more cumbersome to convert the words).
However, for some business operations with high precision, error 0.01 can cause larger problems.
The problem arises because floating-point numbers are inherently imprecise in computers, for reference: http://justjavac.iteye.com/blog/1073775
From the above results, it can be seen that fun2 () and fun4 () are always OK.
Therefore, it is recommended to keep the number of floating-point decimal places in two ways:
①bigdecimal, String construction
②string.format Conversion
Java retains two decimal places