This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# How to round last 2 digits of a double

Rohit Kumar
Ranch Hand
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

Anubhav Anand
Ranch Hand
Posts: 341
I think you'll have to use DecimalFormat class.

Campbell Ritchie
Sheriff
Posts: 48652
56
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
Posts: 48652
56
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.