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

NumberFormat doesn't round correctly in HALF_UP rounding case

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_8

      Description

      A DESCRIPTION OF THE PROBLEM :
      NumberFormat doesn't round correctly in HALF_UP rounding case , see code below in steps to reproduce

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the code below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      FORMAT 0.445 as [0.45]
      -> FORMAT 3.445 as [3.45]
      FORMAT 13.445 as [13.45]
      -> FORMAT 133.445 as [133.45]
      -> FORMAT 7126.445 as [7,126.45]
      ACTUAL -
      FORMAT 0.445 as [0.45]
      FORMAT 3.445 as [3.44]
      FORMAT 13.445 as [13.45]
      FORMAT 133.445 as [133.44]
      FORMAT 7126.445 as [7,126.44]

      ---------- BEGIN SOURCE ----------
      import java.io.IOException;
      import java.math.RoundingMode;
      import java.text.NumberFormat;
      import java.util.Locale;

      public class Formatter {

          public static void main(String[] args) {

              double[] d = new double[]{0.445, 3.445, 13.445, 133.445, 7126.445};
              
              NumberFormat nf = NumberFormat.getInstance(Locale.US);
              nf.setMaximumFractionDigits(2);
              nf.setRoundingMode(RoundingMode.HALF_UP);
              
              for (double v: d) {
                  System.out.println("FORMAT " + v + " as [" + nf.format(v) + "]");
              }
          }
      }

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

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: