File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigDecimal round value ?" Watch "BigDecimal round value ?" New topic

BigDecimal round value ?

Frank Jacobsen
Ranch Hand

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

Andrew Caldwell

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

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

Joined: Oct 13, 2005
Posts: 46425
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:
subject: BigDecimal round value ?
jQuery in Action, 3rd edition