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

Incorrect rounding with String.format()

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      OSX, JDK 12,8

      A DESCRIPTION OF THE PROBLEM :
      Wrong rounding in formatter:
            double v3 = 0.00024649999999999997d;
             System.out.println(String.format("%.6f", v3));
             System.out.println(String.format("%.7", v3));
      0.000247 (incorrect)
      0.0002465

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      double v3 = 0.00024649999999999997d;
             System.out.println(String.format("%.6f", v3));
             System.out.println(String.format("%.7f", v3));

      double v2 = .0002755d;
      System.out.println(new DecimalFormat("#.######").format(v2));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      0.000246
      ACTUAL -
      0.000247

      ---------- BEGIN SOURCE ----------
      double v3 = 0.00024649999999999997d;
      System.out.println(String.format("%.6f", v3));
      System.out.println(String.format("%.7f", v3));
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      BigDecimal maybe, but it's slow

      FREQUENCY : always


            rgiulietti Raffaello Giulietti
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: