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

java.math.BigDecimal has incorrect implementation of ROUND_HALF_EVEN

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.1
    • core-libs
    • x86
    • windows_xp



      Name: rmT116609 Date: 05/14/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
      Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)

      java version "1.4.2-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b19)
      Java HotSpot(TM) Client VM (build 1.4.2-beta-b19, mixed mode)

      FULL OS VERSION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      java.math.BigDecimal does not implement Banker's Rounding (round half to even) correctly. Some values will not be rounded correctly, such as
        1.15 rounded to 1 decimal
        1.275 rounded to 2 decimals

      This is probably the root cause to Bug ID 4848133 "NumberFormat.format() not using ROUND_HALF_EVEN".

      The bug is a huge problem when implementing an accounting system. Workaround would be highly appreciated.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run provided source code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected output:
      1.28
      1.24
      2.26
      1.2
      1.2
      ACTUAL -
      Actual output:
      1.27
      1.24
      2.25
      1.1
      1.2

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.math.BigDecimal;

      public class BigDecimalBug
      {
        public static void main(String[] args)
        {
          System.out.println(new BigDecimal(1.275).setScale(2,BigDecimal.ROUND_HALF_EVEN).toString());
          System.out.println(new BigDecimal(1.235).setScale(2,BigDecimal.ROUND_HALF_EVEN).toString());
          System.out.println(new BigDecimal(2.255).setScale(2,BigDecimal.ROUND_HALF_EVEN).toString());

          System.out.println(new BigDecimal(1.15).setScale(1,BigDecimal.ROUND_HALF_EVEN).toString());
          System.out.println(new BigDecimal(1.25).setScale(1,BigDecimal.ROUND_HALF_EVEN).toString());
        }
      }

      ---------- END SOURCE ----------
      (Review ID: 185793)
      ======================================================================

            darcy Joe Darcy
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: