// Method 1:
double f = 3.1516;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
// Method 2:
new java.text.DecimalFormat("#.00").format(3.1415926);
// #.00 means two decimal places #.0000 four decimal places and so on…
// Method 3:
double d = 3.1415926;
String result = String.format("%.2f", d);
// %.2f %. Indicates any digit before the decimal point. The result after two decimal format is f. Indicates floating point type.
//Method 4:
Math.round(5.2644555 * 100) * 0.01d;
//String.format("%0" + 15 + "d", 23) 23 is less than 15, and it is added to the previous one.
The above is the full content of the implementation method of rounding and retaining decimals brought to you by the editor. I hope it will be helpful to everyone and support Wulin.com more~