wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes BigDecimal round value ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigDecimal round value ?" Watch "BigDecimal round value ?" New topic
Author

BigDecimal round value ?

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 348
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

Joined: Aug 18, 2008
Posts: 1
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
And as Andrew has told you a BigDecimal is immutable so the original reference will still maintain the .2999999999999999 bit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigDecimal round value ?
 
Similar Threads
BigDecimal rounding problem
how to use ceil() method for a bigdecimal type value in java?
urgent
Rounding BigDecimal Calculation Yielded Wrong Result
Rounding problem