Michael
Michael

Reputation: 14218

Rounding to 2 decimal places

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

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

Upvotes: 6

Views: 6437

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500065

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.

Upvotes: 7

Bala R
Bala R

Reputation: 108937

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

Upvotes: 3

Related Questions