aspose file tools*
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
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: 40064
    
  28
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: 40064
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to round last 2 digits of a double