I'm trying to implement a new grade rounding to BigDecimal class, and I'm getting a possible bug, must probably I doing something wrong. The code below exposes my problem:
public static void main(String[] args) throws IOException {
BigDecimal valDouble = new BigDecimal(0.35);
valDouble = valDouble.setScale(1, BigDecimal.ROUND_HALF_UP);
System.out.println(valDouble.doubleValue()); // prints 0.3
BigDecimal valString = new BigDecimal(new String("0.35"));
valString = valString.setScale(1, BigDecimal.ROUND_HALF_UP);
System.out.println(valString.doubleValue()); // prints 0.4
}
My doubt here is, is BigDecimal different for double and String constructors?
I can't understand this 'bug', at least, I just used a simple string concat to 'solve' it, as below:
BigDecimal valDouble = new BigDecimal("" + 0.35);
Any idea what could be causing this odd behavior?