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

CompactNumberInstance.format incorrectly formats some numbers (few vs many)

XMLWordPrintable

    • b13
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      The logic in java.text.CompactNumberFormat#format(long, java.lang.StringBuffer, java.text.Format.FieldDelegate) incorrectly interprets numbers - in specific conditions the value calculated to determine prefix and suffix will differ from the value which will be displayed. Due to this incorrect suffix is picked (one vs few, few vs many).

      Some examples:
      - 4949 in pl_PL returns "5 tysiące" instead of "5 tysięcy"
      - 1949 in Locale.FRANCE returns "2 millier" instead of "2 mille"
      - 1949 in Locale.ITALIAN returns "mille" instead of "2 mila"

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Format number 4949 using pl_PL locale:
      NumberFormat.getCompactNumberInstance(new Locale("pl", "PL"), NumberFormat.Style.LONG).format(4949);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      5 tysięcy
      ACTUAL -
      5 tysiące

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

      public class TestCase {
          public static void main(String[] args) {
              var pl = new Locale("pl", "PL");
              var cn = NumberFormat.getCompactNumberInstance(pl, NumberFormat.Style.LONG);

              var correct = cn.format(5000); // 5 tysięcy
              var incorrect = cn.format(5000 - 51); // 5 tysiące

              if (!correct.equals(incorrect)) {
                  throw new IllegalStateException("The value '%s' was expected to be '%s'!".formatted(
                          incorrect,
                          correct
                  ));
              } else {
                  System.out.printf("Values are the same: %s%n", correct);
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            naoto Naoto Sato
            pnarayanaswa Praveen Narayanaswamy
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: