Win a copy of Functional Reactive Programming this week in the Other Languages 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

Thnaks in advance

Anubhav Anand
Ranch Hand
Posts: 341
I think you'll have to use DecimalFormat class.
Please refer this link for more details.

Campbell Ritchie
Sheriff
Posts: 50264
79
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: 50264
79
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.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?