Round to 2 decimal places

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

Is there a better way to round decimals to two decimal places?

+6
source share
2 answers
 DecimalFormat format=new DecimalFormat("#.##"); System.out.println(format.format(doubleValue)); 
+3
source

If you are interested in decimals and therefore exact decimal values, you should usually use java.math.BigDecimal . You can then use Decimal.round or Decimal.setScale to round to suit your exact needs.

+7
source

Source: https://habr.com/ru/post/891459/


All Articles