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

NumberFormat.getNumberInstance() uses RoundingMode.HALF_EVEN instead of HALF_UP

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 6u21
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.6.0_20"
      Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux 2.6.32-24-generic #39-Ubuntu SMP Wed Jul 28 05:14:15 UTC 2010 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      NumberFormat.getNumberInstance() returns a NumberFormat instance that uses RoundingMode.HALF_EVEN as its default rounding method.
      This is IMHO not a general-purpose number format since rounding values like 10.5d will result to 10 instead of 11!
      The default rounding method should be RoundingMode.HALF_UP!

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the simple example

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      see example
      ACTUAL -
      see example

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
           NumberFormat nf = NumberFormat.getNumberInstance(); // same using static method taking Locale
           ((DecimalFormat)nf).applyPattern("#0"); // round
      // nf.setRoundingMode(RoundingMode.HALF_UP);
           System.err.println("12.5: "+nf.format(12.5d)+", but should be 13!");
           System.err.println("11.5: "+nf.format(12.5d)+", ok");

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      setRoundingMode(RoundingMode.HALF_UP);

      SUPPORT :
      YES

            peytoia Yuka Kamiya (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: