Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpredictability of the BigDecimal(double) constructor

Tags:

I started using Sonar recently in a project, and i got a PMD rule broken about using the constructor new BigDecimal(double val). When i read the java documentation, i found that new BigDecimal(double val) is somewhat unpredictable and that I should use new BigDecimal(String val) which is predictable.

Here is what javadoc says for BigDecimal public BigDecimal(double val):

Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is an integer.

Notes:

The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.

When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.

Why does this constructor really exists? Isnt new BigDecimal(String val) enough for that matter? When should I use the new BigDecimal(double val) constructor?

like image 1000
gdfbarbosa Avatar asked Aug 31 '12 15:08

gdfbarbosa


People also ask

How accurate is BigDecimal?

This limits it to 15 to 17 decimal digits of accuracy. BigDecimal can grow to any size you need it to. Double operates in binary which means it can only precisely represent numbers which can be expressed as a finite number in binary. For example, 0.375 in binary is exactly 0.011.

How do I stop rounding in BigDecimal?

56 if you use BigDecimal newValue = myBigDecimal. setScale(2, RoundingMode. DOWN); " This statement is true for HALF_DOWN not for DOWN mode.

What can I use instead of BigDecimal?

If you need to use division in your arithmetic, you need to use double instead of BigDecimal.


2 Answers

Why does this constructor really exists?

It converts the actual represented value of double to a BigDecimal. The whole point of BigDecimal is to give as much precision as possible and that is what this constructor does.

If you want to take the value you would get with a small amount of rounding the Double.toString(double) uses you can use

System.out.println(BigDecimal.valueOf(0.1));

prints

0.1

When should I use the new BigDecimal(double val) constructor

When you want to know the value double really represents. You can apply your own rounding as required.

When you use double you should always apply a sensible rounding. But, if you did that you may find you don't need BigDecimal. ;)

like image 55
Peter Lawrey Avatar answered Oct 06 '22 00:10

Peter Lawrey


When should I use the new BigDecimal(double val) constructor?

Preferably - nowhere.

If you are willing to play with BigDecimals, using constructor with double means loosing all benefits of exact number presentation.

Why does this constructor really exists?

Because sometimes you have only double, that you want to translate to BigDecimal. Forcing you to use .toString() inbetween would be just silly ;)

like image 22
dantuch Avatar answered Oct 06 '22 00:10

dantuch