It's not a secret anymore!
The moose likes Beginning Java and the fly likes rounding a double value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "rounding a double value" Watch "rounding a double value" New topic

rounding a double value

Alex Dubin

Joined: Jul 04, 2005
Posts: 12

I have a double value (1.63722333) and I need to round it to the nearest tenth so I get 1.64. How can I do this?

Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

Hi Alex

Alex Dubin

Joined: Jul 04, 2005
Posts: 12
cool, thanks!
Tom McC

Joined: Jul 28, 2005
Posts: 21
Just to clarify, Math.round() will always round to the nearest whole number. The reason Kundret's code rounds to the nearest hundredth is because we FIRST multiply it by 100, then, after rounding it, divide by 100 again. This way, you can round to any place, as long as you move the decimal point (by multiplying/dividing) before and after the rounding.
Andrew Morris

Joined: Aug 27, 2005
Posts: 8
Another alternate to round a double value, make a java.text.DecimalFormat like:
static DecimalFormat df=new DecimalFormat("0.00");
and it does the rounding.

Andrew Morris
DevSquare - Online Application Development
I agree. Here's the link:
subject: rounding a double value
jQuery in Action, 3rd edition