rounding error in BigDecimal.setScale

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P2
    • None
    • Affects Version/s: 1.4.0
    • Component/s: 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)
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: