This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes BigDecimal round value ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "BigDecimal round value ?" Watch "BigDecimal round value ?" New topic
Author

BigDecimal round value ?

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 335
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: 32651
    
    4
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: 32651
    
    4
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
Rounding BigDecimal Calculation Yielded Wrong Result
BigDecimal rounding problem
Rounding problem
urgent
how to use ceil() method for a bigdecimal type value in java?