Implementation of Java rounding and keeping decimal

//Mode 1:

double f = 3.1516; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP). doubleValue();

//Mode 2:

new java. text. DecimalFormat("#.00"). format(3.1415926); // #. 00 means two decimal places #. 0000, four decimal places, and so on

//Mode 3:

double d = 3.1415926; String result = String. format("%.2f",d); // %. 2f %. Represents any number of digits before the decimal point, 2 represents two decimal places, and the result after the format is f represents floating point type.

//Method 4:

Math. round(5.2644555 * 100) * 0.01d; // String. Format ("% 0" + 15 + "d", 23) if 23 is less than 15, fill 0 in front

The above is the whole content of the implementation method of Java rounding and keeping decimals brought by Xiaobian. I hope it will help you and support programming tips~

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
分享
二维码
< <上一篇
下一篇>>