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 documentat
double val = 0.1; // this is not 0.1, but an approximination as pointed out in other answers.
BigDecimal biggy = new BigDecimal(val); // this variable conatains the "same" value as val does, however it is not 0.1, but as pointed out in other answers: 0.100000000000000005551115123`
So, in the case listed here, where you have full control over the source code, you should write the val as 'String val = "0.1"'
However the contructor BigDecimal(double val)
is usefull in case you read binary doubles from a file. Obviously you will want in nearly all cases BigDecimals that are exactly the same as the 'doubles'
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. ;)
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 ;)
Because if you already begin with a double
value as data, you have already lost that precission. So not having it would force you to convert it to String
for BigDecimal
to convert it back.
And also, maybe sometimes you just need that value.
Unpredictable? I disagree.
final double before = 0.1;
BigDecimal bd = new BigDecimal(before);
double after = bd.doubleValue();
assert before == after;
Key to understanding:
assert 0.1 == 0.1000000000000000055511151231257827021181583404541015625;
If you truly meant "0.1", then yes, use the String constructor =) But if you have a double
already, then that double is not "0.1".
The JavaDoc you quoted says:
When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion;
I fail to see how an exact conversion is "unpredictable" and I am very much in favor of removing the word "must" from this quote.
At the end of the day, both constructors work just like intended. If you have String "0.1", then BigDecimal(String) convert this value exactly. If you have a double 0.1, then BigDecimal(double) convert this value exactly.
You would use the double constructor when the data you want to work with already exists as a double value. Having to convert it to a string before converting it to BigDecimal would be a wasteful in that case.