开发者

Rounding to 2 decimal places

开发者 https://www.devze.com 2023-03-16 03:26 出处:网络
(Math.round(doubleValue*100))/100.0 Is there a better way to ro开发者_C百科und decimals to 2 decimal places?If you\'re interested in decimal places and therefore precise decimal values, you should t
(Math.round(doubleValue*100))/100.0

Is there a better way to ro开发者_C百科und decimals to 2 decimal places?


If you're interested in decimal places and therefore precise decimal values, you should typically be using java.math.BigDecimal to start with. You can then use Decimal.round or Decimal.setScale to round according to your exact needs.


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

精彩评论

暂无评论...
验证码 换一张
取 消