| Author |
BigDecimal() issue
|
aydeniz kirmizi
Ranch Hand
Joined: Aug 20, 2009
Posts: 35
|
|
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: 8428
|
|
|
because your literal of 0.0075 is first converted into a float, which are never precise.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18370
|
|
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
How To Ask Questions How To Answer Questions
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18370
|
|
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: 35
|
|
|
Thanks a lot, fixed..
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 8428
|
|
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: 35
|
|
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: 35
|
|
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: 2686
|
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 11642
|
|
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 7 API documentation
Scala Notes - My blog about Scala
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 11642
|
|
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: 35
|
|
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: 547
|
|
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.
|
OCPJP 6
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 1607
|
|
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
|
More computing sins are committed in the name of efficiency (without necessarily achieving it)
than for any other single reason...including blind stupidity. — W.A. Wulf
|
 |
aydeniz kirmizi
Ranch Hand
Joined: Aug 20, 2009
Posts: 35
|
|
Hey,
Thanks a lot for sharing your knowladge. toPlainString(), is ersolved my issue.
|
 |
 |
|
|
subject: BigDecimal() issue
|
|
|