File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "rounding a double value" Watch "rounding a double value" New topic
Author

rounding a double value

Alex Dubin
Greenhorn

Joined: Jul 04, 2005
Posts: 12
Hello,

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?

thanks,
Alex
Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

Hi Alex


Cheers
Alex Dubin
Greenhorn

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

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
Greenhorn

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");
use
System.out.println(df.format(aDoubleValue));
and it does the rounding.

-----------------
Andrew Morris
www.devsquare.com
DevSquare - Online Application Development
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: rounding a double value