How is it that Java’s BigDecimal can be this painful?
Double d = 13.3D; BigDecimal bd1 = new BigDecimal(d); BigDecimal bd2 = new BigDecimal(String.valueOf(d)); System.out.println('RESULT 1: '+bd1.toString()); System.out.println('RESULT 2: '+bd2.toString()); RESULT 1: 13.300000000000000710542735760100185871124267578125 RESULT 2: 13.3
Is there any situation where Result 1 would be desired? I know that Java 1.5 changed the toString() method but was this the intended consequence?
Also I realise that BigDecimal has doubleValue() etc, but the library that I am working with helpfully uses a toString() and I can’t change that 🙁
Cheers.
Well, the API does address this apparent inconsistency in the constructor
BigDecimal(double val):Moral of the story: The pain seems self-inflicted, just use
new BigDecimal(String val)orBigDecimal.valueOf(double val)instead =)