Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8052366

NumberFormat doesn't round correctly in HALF_UP rounding case

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Duplicate
    • Affects Version/s: 8u11
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux

      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 ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                olagneau Olivier Lagneau (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: