File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Round a double to 2 places of Decimal Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Round a double to 2 places of Decimal" Watch "Round a double to 2 places of Decimal" New topic
Author

Round a double to 2 places of Decimal

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
I have a double value which is multiplied by another value. I want that the result be in only 2 places of decimal rather than the unnecessary precision i am getting.
please suggest.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
You can use java.text.NumberFormat class to do so.
You can find the help regarding that from :
http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html
Rikko Verrijzer
Ranch Hand

Joined: Jul 22, 2003
Posts: 34
Hi,
Perhaps the most simple, but not very subtle solution would be: multiply by 100, call Math.round(double), cast to double and divide by 100.
long story short, something like this:
roundeDoubleValue = (double)Math.round(someDoubleValue*100)/100;
But there might be a better solution though
Rikko
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Round a double to 2 places of Decimal
 
Similar Threads
Setting precision for a double value
How to find number of decimal places?
Decimal formatting of
Decimal formatting of
double precision