aspose file tools*
The moose likes Beginning Java and the fly likes BigDecimal() issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigDecimal() issue" Watch "BigDecimal() issue" New topic
Author

BigDecimal() issue

aydeniz kirmizi
Ranch Hand

Joined: Aug 20, 2009
Posts: 39
private BigDecimal stakeContrib;

this.number = new BigDecimal(0.0075);

is returning 0.007499999999999999722444243843710864894092082977294921875

any idea why?

Thanks a lot.


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

because your literal of 0.0075 is first converted into a float, which are never precise.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

aydeniz kirmizi wrote:this.number = new BigDecimal(0.0075);

On that line, the double is already inaccurate; see our Java Beginner's FAQ entry #20.

The solution is very simple - use a String to create the BigDecimal:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

fred rosenberger wrote:because your literal of 0.0075 is first converted into a float, which are never precise.

Actually it remains a double, but your point is still valid.
aydeniz kirmizi
Ranch Hand

Joined: Aug 20, 2009
Posts: 39
Thanks a lot, fixed..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

Rob Spoor wrote:Actually it remains a double, but your point is still valid.

my bad...doing too many things at once...perhaps should have said "floating decimal point type"...or just 'a double'.
aydeniz kirmizi
Ranch Hand

Joined: Aug 20, 2009
Posts: 39
Thanks for your help. I do have another question, the BigDecimals seem to be a bit complicated.



this is printing me out 0.7500 , but the value I want to get is 0.75. Any suggestion for that?

thanks
aydeniz kirmizi
Ranch Hand

Joined: Aug 20, 2009
Posts: 39
got it!

setScale(2)

but if there is a better option you can suggest, would be hallpy.
Thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
String.format can be used to format numbers in a String but I'm not sure if it works with BigDecimals.


Joanne
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Yes, String.format also works with BigDecimal:

It uses the decimal separator of your default locale though, so in my case I get "0,0075" (with a comma instead of a dot). If you want always a dot you can specify for example to use the US locale:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

aydeniz kirmizi wrote:but if there is a better option you can suggest, would be hallpy.

BigDecimal also has a stripTrailingZeroes method:
aydeniz kirmizi
Ranch Hand

Joined: Aug 20, 2009
Posts: 39
when using "stripTrailingZeros()", it is strange but for instance for 0.5 value it displays 5E+1, for 0.1 it displays 1E+1.
What a pain!



I want this to be displayed as it is. I mean when I multiply a value with 100, I just want to get whatever the result is. setScale(2) is not working too, because if I had something like 0.0562 it throws exception, because the scale is forced to be 2.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
aydeniz kirmizi wrote:setScale(2) is not working too, because if I had something like 0.0562 it throws exception, because the scale is forced to be 2.


The exception is thrown because you haven't told the BigDecimal how it should handle the excess values. You need to supply a rounding mode to the setScale method. The javadoc is quite clear about this.

Note that if you require a consistent number of decimal places, use setScale and not stripTrailingZeros, because it will pad or round to the desired number of places. If you don't want a fixed number of decimal places, then you shouldn't be using setScale at all.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

aydeniz kirmizi wrote:when using "stripTrailingZeros()", it is strange but for instance for 0.5 value it displays 5E+1, for 0.1 it displays 1E+1.
What a pain!

Nothing to do with stripTrailingZeros(), but I agree that it's a pain: The only example I can ever remember of Java changing the way a method works from one release to another.

What you want is toPlainString(), not toString().

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
aydeniz kirmizi
Ranch Hand

Joined: Aug 20, 2009
Posts: 39
Hey,

Thanks a lot for sharing your knowladge. toPlainString(), is ersolved my issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigDecimal() issue