JDK-8338242 : RoundingMode.HALF_UP gives different results with NumberFormat
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 22.0.2
  • Priority: P3
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: linux_ubuntu
  • CPU: x86_64
  • Submitted: 2024-08-12
  • Updated: 2024-08-15
  • Resolved: 2024-08-14
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
RoundingMode.HALF_UP gives different results with NumberFormat

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the provided code. You will see the following output:

0.675 -> 0.68
12345.675 -> 12,345.67

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
0.675 -> 0.68
12345.675 -> 12,345.68

---------- BEGIN SOURCE ----------
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
numberFormat.setMaximumFractionDigits(2);
numberFormat.setMinimumFractionDigits(2);
numberFormat.setRoundingMode(RoundingMode.HALF_UP);

System.out.println("0.675 -> " + numberFormat.format(0.675));
System.out.println("12345.675 -> " + numberFormat.format(12345.675));
---------- END SOURCE ----------

FREQUENCY : always



Comments
A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/20580 Date: 2024-08-14 11:46:31 +0000
15-08-2024

The closest binary representation of 12345.675d is: ``` jshell> new BigDecimal(12345.675).toString() $179 ==> "12345.67499999999927240423858165740966796875" ``` Thus when rounding at the 3rd minor digit, it will end up with 12345.67.
14-08-2024