Reprinted: https://blog.csdn.net/xue_feitian/article/details/6556275
the first method:
. 1 Double F = 123.2315455458 ; 2 the BigDecimal B = new new the BigDecimal (F); . 3 Double F1 = b.setScale (. 3, BigDecimal.ROUND_HALF_UP) .doubleValue (); // three decimal places, and finally a rounding . 4 the System.out .println (f1);
Output: 123.232
The second method:
1 DecimalFormat df = new new DecimalFormat (. "# 000"); // # .000 three decimal places, and so on 2 String f = df.format (123.2315452); // rounding 3 System.out.println (f);
Output: 123.232
The third method:
. 1 Double D = 123.2315455458 ; 2 String String.format S = ( "% 6F.", D); // Reserved six decimal places, the last bit rounded . 3 System.out.println (S);
The output is: 123.231546
% Represents any number of digits before the decimal point 6 shows the results after two decimal floating-point representation format is f