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

Formatting a decimal using locale-specific grouping separators causes ArithmeticException (division by zero).

XMLWordPrintable

    • b07
    • x86_64
    • linux
    • Verified

      FULL PRODUCT VERSION :
      openjdk version "9.0.4"
      OpenJDK Runtime Environment (build 9.0.4+11)
      OpenJDK 64-Bit Server VM (build 9.0.4+11, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux lethe 4.14.13-1-ARCH #1 SMP PREEMPT Wed Jan 10 11:14:50 UTC 2018 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      (This bug report is based on https://bugs.openjdk.java.net/browse/JDK-8167567, which was closed because it was not reproducible. This report includes a reproducible test case and a link to a fix.)

      String.format("%,d", 1000) throws an exception when the default locale use a digit grouping size of 0.

      The same thing happens when passing such a locale to the overload of String.format() that takes an explicit Locale argument.

      The test case below uses the Armenian locale as an example that exhibits this problem.

      The problem was also present in the JDK implementation that ships with Android, which is based on OpenJDK, and it was fixed there as follows: https://android.googlesource.com/platform/libcore/+/558e9c5%5E%21/

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Save executable test case as Test.java, then compile with `javac Test.java` and run with `java Test`.


      ACTUAL -
      Exception in thread "main" java.lang.ArithmeticException: / by zero
      at java.base/java.util.Formatter$FormatSpecifier.localizedMagnitude(Formatter.java:4403)
      at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:3155)
      at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:3140)
      at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2840)
      at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2800)
      at java.base/java.util.Formatter.format(Formatter.java:2581)
      at java.base/java.util.Formatter.format(Formatter.java:2517)
      at java.base/java.lang.String.format(String.java:2788)
      at Test.main(Test.java:6)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Locale;

      class Test {
          public static void main(String... args) {
              Locale locale = new Locale("hy", "AM");
              System.out.println(String.format(locale, "%,d", 1000));
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Either don't use digit grouping, or pass in an explicit locale that doesn't exhibit this problem (e.g. Locale.US).

            nishjain Nishit Jain
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: