This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to round last 2 digits of a double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to round last 2 digits of a double" Watch "How to round last 2 digits of a double" New topic
Author

How to round last 2 digits of a double

Rohit Kumar
Ranch Hand

Joined: Sep 20, 2007
Posts: 53
Hi,

Is there any function to round last 2 digits of double.
For example,

I have double value, 0.8357 and i want 0.8300
I have double value, -1.3532 and i want -1.3500

Thnaks in advance
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

I think you'll have to use DecimalFormat class.
Please refer this link for more details.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
For rounding, use the Math.round() method. It only rounds to a whole number, so multiply by 100d and round and divide by 100d.
To print out 12.3400 use the methods which use % tags, eg printf or format. Details in the Formatter class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
By the way, you are not using the same rounding convention as in Math.round(). For round-toward-zero, multiply by 100, cast to an int or long and divide by 100d.
 
Don't get me started about those stupid light bulbs.
 
subject: How to round last 2 digits of a double
 
Similar Threads
Round double up with 2 digits....
Double Value
String to a Double (A better way?)
BigDecimal round value ?
How to get digits from a number using MessageFormat