<<< MOST COMMON MISTAKE WITH BigDecimal | Index | Comparing BigDecimals >>> |
To create a BigDecimal object from another BigDecimal object, use
BigDecimal total2 = new BigDecimal(total.toString());
Notice the use of toString. When converting to/from double instead of the String, the BigDecimal often gets an unwanted (and unnecessary) high precision fractions.
For example, a double value of 1.4 becomes
double value: 1.4 BigDecimal: 1.399999999999999911182158029987476766109466552734375
Try to uncomment and use nextDouble method in the sample program Rounding.java to test this behavior.
<<< MOST COMMON MISTAKE WITH BigDecimal | Index | Comparing BigDecimals >>> |