The DeciamlFormat with RoundingMode.HALF_UP is not working correctly with double value.
double value = 2.315;
double value2 = 2.325;
DecimalFormat df = new DecimalFormat("#.00");
System.out.println("double value1 = " + value);
System.out.println("double value2 = " + value2);
df.setRoundingMode(RoundingMode.HALF_UP);
System.out.println(df.getRoundingMode() + " value = " + df.format(value));
System.out.println(df.getRoundingMode() + " value2 = " + df.format(value2));
df.setRoundingMode(RoundingMode.HALF_EVEN);
System.out.println(df.getRoundingMode() + " value = " + df.format(value));
System.out.println(df.getRoundingMode() + " value2 = " + df.format(value2));
The code above generate the result below, HALF_UP 2.315d result as same as HALF_EVEN.
double value1 = 2.315
double value2 = 2.325
HALF_UP value = 2.31 // Why the result is not 2.32?
HALF_UP value2 = 2.33
HALF_EVEN value = 2.31
HALF_EVEN value2 = 2.33
But DeciamlFormat with RoundingMode.HALF_UP format "2.315 float value " will be the correct value 2.32.
I work with JDK 1.8.0_131 version.