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

rounding error in BigDecimal.setScale

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P2 P2
    • None
    • 1.4.0
    • core-libs
    • generic
    • generic



      Name: nt126004 Date: 09/27/2001


      java version "1.4.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)


      java.math.BigDecimal using ROUND_HALF_UP does not behave as expected

      i.e

      import java.math.*;
      public class BigTest {
      public static void main(String[] args) {

          BigDecimal bd = new BigDecimal(0.115);
          bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
          System.out.println("? 0.12 = " + bd);

          bd = new BigDecimal(1.115);
          bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
          System.out.println("? 1.12 = " + bd);

          bd = new BigDecimal(2.115);
          bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);

          System.out.println("? 2.12 = " + bd);
      }
      }



      output:

      ? 0.12 = 0.12
      ? 1.12 = 1.11
      ? 2.12 = 2.12

      should be:

      ? 0.12 = 0.12
      ? 1.12 = 1.12
      ? 2.12 = 2.12


      (Review ID: 132704)
      ======================================================================

            darcy Joe Darcy
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: