Here is the code:
public static void main(String[] args) {
final double d1 = 811.440000;
final double d2 = 425.530000;
final double d3 = 384.270000;
for (double d : Arrays.asList(d1, d2, d3)) {
final String dstr = String.format("%f", d);
BigDecimal bg1 = BigDecimal.valueOf(d).setScale(2, BigDecimal.ROUND_DOWN);
BigDecimal bg2 = (new BigDecimal(dstr)).setScale(2, BigDecimal.ROUND_DOWN);
BigDecimal bg3 = (new BigDecimal(d)).setScale(2, BigDecimal.ROUND_DOWN);
System.out.printf("[%s : %f] {%f, %f} %f\n", dstr, d, bg1, bg2, bg3);
}
}
Here is the output:
[811.440000 : 811.440000] {811.440000, 811.440000} 811.440000
[425.530000 : 425.530000] {425.530000, 425.530000} 425.520000
[384.270000 : 384.270000] {384.270000, 384.270000} 384.260000
Why don't we change valueOf(double) method or the BigDecimal(double) constructor of the BigDecimal class so as to get a consistent result?