Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to round last 2 digits of a double

 
Rohit Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you'll have to use DecimalFormat class.
Please refer this link for more details.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50264
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50264
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic