简体   繁体   English

舍入到小数点后两位

[英]Rounding to 2 decimal places

(Math.round(doubleValue*100))/100.0

有没有更好的方法将小数舍入到2位小数?

If you're interested in decimal places and therefore precise decimal values, you should typically be using java.math.BigDecimal to start with. 如果您对小数位和精确的十进制值感兴趣,通常应该使用java.math.BigDecimal开头。 You can then use Decimal.round or Decimal.setScale to round according to your exact needs. 然后,您可以根据您的确切需要使用Decimal.roundDecimal.setScale进行舍入。

DecimalFormat format=new DecimalFormat("#.##");
System.out.println(format.format(doubleValue));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM