Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal round value ?

 
Frank Jacobsen
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a BigDecimal Value, with the value 23.2999999764 i want to round this value to 2 digits after the decimal point so the value is 23.30 but i want to keep the value in my BigDecimal , is this possible, and if,
how ?


Frank
 
Andrew Caldwell
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They're immutable so if you call .scale on it, you'll return a new BigDecimal that's the same but to two decimal places.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Andrew Caldwell

Frank Jacobsen, please read this thread which discusses a similar problem. You have probably instantiated your BigDecimal from a double, which immortalises and maintains the imprecision of floating-point arithmetic. You would be better to use a String to instantiate the BigDecimal.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as Andrew has told you a BigDecimal is immutable so the original reference will still maintain the .2999999999999999 bit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic