ADDITIONAL SYSTEM INFORMATION :
Tested with jdk1.8.0_241 and jdk11
A DESCRIPTION OF THE PROBLEM :
When setting pattern "0.0" into DecimalFormat and formatting double number "40.55" the result is "40.5" but should be "40.6". For other numbers, e.g. "30.55" the result is correct as "30.6".
Bug is similar toJDK-7131459 which is noted as fixed.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code provided in the source code sections. Or use this shortcut
"new DecimalFormat("0.0").format(40.55d)"
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
30.5 --> 30.5
30.55 --> 30.6
30.555 --> 30.6
40.5 --> 40.5
40.55 --> 40.6 <--- expected
40.555 --> 40.6
ACTUAL -
30.5 --> 30.5
30.55 --> 30.6
30.555 --> 30.6
40.5 --> 40.5
40.55 --> 40.5 <---- actual
40.555 --> 40.6
---------- BEGIN SOURCE ----------
import java.math.RoundingMode;
import java.text.DecimalFormat;
public final class TestDecimalFormatter {
public static void main(String[] args) {
DecimalFormat formatter = new DecimalFormat("0.0");
formatter.setRoundingMode(RoundingMode.HALF_UP);
double[] numbers = new double[] { 30.5, 30.55, 30.555, 40.5, 40.55, 40.555 };
for (double d : numbers) {
System.out.println("" + d + " --> " + formatter.format(d));
}
}
}
---------- END SOURCE ----------
FREQUENCY : always
Tested with jdk1.8.0_241 and jdk11
A DESCRIPTION OF THE PROBLEM :
When setting pattern "0.0" into DecimalFormat and formatting double number "40.55" the result is "40.5" but should be "40.6". For other numbers, e.g. "30.55" the result is correct as "30.6".
Bug is similar to
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code provided in the source code sections. Or use this shortcut
"new DecimalFormat("0.0").format(40.55d)"
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
30.5 --> 30.5
30.55 --> 30.6
30.555 --> 30.6
40.5 --> 40.5
40.55 --> 40.6 <--- expected
40.555 --> 40.6
ACTUAL -
30.5 --> 30.5
30.55 --> 30.6
30.555 --> 30.6
40.5 --> 40.5
40.55 --> 40.5 <---- actual
40.555 --> 40.6
---------- BEGIN SOURCE ----------
import java.math.RoundingMode;
import java.text.DecimalFormat;
public final class TestDecimalFormatter {
public static void main(String[] args) {
DecimalFormat formatter = new DecimalFormat("0.0");
formatter.setRoundingMode(RoundingMode.HALF_UP);
double[] numbers = new double[] { 30.5, 30.55, 30.555, 40.5, 40.55, 40.555 };
for (double d : numbers) {
System.out.println("" + d + " --> " + formatter.format(d));
}
}
}
---------- END SOURCE ----------
FREQUENCY : always
- duplicates
-
JDK-8252988 Wrong DecimalFormat.format() HALF_UP rounding when last digit is 5
-
- Closed
-