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. ;)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…