JDK-8052366 : NumberFormat doesn't round correctly in HALF_UP rounding case
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 8u11
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86_64
  • Submitted: 2014-07-25
  • Updated: 2014-10-09
  • Resolved: 2014-07-30
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux nelson 3.14.9-200.fc20.x86_64 #1 SMP Thu Jun 26 21:40:51 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
NumberFormat doesn't round correctly in some corner cases.

Formatting the value 0.026 with maxFractionDigits=2 and RoundingMode.HALF_UP results in "0.02".

See the failing test case provided in "Steps to Reproduce"

REGRESSION.  Last worked in version 7u60

ADDITIONAL REGRESSION INFORMATION: 
java version "1.7.0_60"
OpenJDK Runtime Environment (fedora-2.5.0.1.fc20-x86_64 u60-b30)
OpenJDK 64-Bit Server VM (build 24.60-b09, mixed mode)


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Failing test:

    NumberFormat format = DecimalFormat.getInstance( Locale.ENGLISH );
    format.setMaximumFractionDigits( 2 );
    format.setMinimumFractionDigits( 2 );
    format.setRoundingMode( RoundingMode.HALF_UP );
    Assert.assertEquals( "0.02", format.format( 0.024 ) );
    Assert.assertEquals( "0.03", format.format( 0.025 ) );
    Assert.assertEquals( "0.03", format.format( 0.026 ) );     // <- Fails! Results in "0.02"

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Formatting the value 0.026 with maxFractionDigits=2 and RoundingMode.HALF_UP should result in "0.03".
ACTUAL -
Formatting the value 0.026 with maxFractionDigits=2 and RoundingMode.HALF_UP results in "0.02".

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
    NumberFormat format = DecimalFormat.getInstance( Locale.ENGLISH );
    format.setMaximumFractionDigits( 2 );
    format.setMinimumFractionDigits( 2 );
    format.setRoundingMode( RoundingMode.HALF_UP );
    Assert.assertEquals( "0.02", format.format( 0.024 ) );
    Assert.assertEquals( "0.03", format.format( 0.025 ) );
    Assert.assertEquals( "0.03", format.format( 0.026 ) );     // <- Fails! Results in "0.02"
---------- END SOURCE ----------


Comments
Closing it as duplicate
30-07-2014

Hi Naoto, quickly looked at it. This is not a behavorial change that would be due to JDK-7131459, but it is a duplicate of JDK-8039915.
30-07-2014

@Olivier, Looks like a dup of 7131459. Please close it if it is a dup. Thanks.
28-07-2014