Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding to 2 decimal places

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

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

like image 943
Michael Avatar asked Jun 27 '11 14:06

Michael


1 Answers

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.

like image 132
Jon Skeet Avatar answered Sep 28 '22 08:09

Jon Skeet