File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal() issue

 
aydeniz kirmizi
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private BigDecimal stakeContrib;

this.number = new BigDecimal(0.0075);

is returning 0.007499999999999999722444243843710864894092082977294921875

any idea why?

Thanks a lot.


 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because your literal of 0.0075 is first converted into a float, which are never precise.
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, fixed..
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it!

setScale(2)

but if there is a better option you can suggest, would be hallpy.
Thanks
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.format can be used to format numbers in a String but I'm not sure if it works with BigDecimals.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
aydeniz kirmizi
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

Thanks a lot for sharing your knowladge. toPlainString(), is ersolved my issue.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic