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

Compact Number Formatting support

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 12
    • core-libs
    • None
    • behavioral
    • minimal
    • Hide
      Since most of the APIs are newly added, the compatibility risk is minimal. However, two new fields are added to the java.text.NumberFormat.Field which is a serializable class and deserializing one of the new fields by an old runtime may throw an exception (as readResolve() throw an exception for unknown names), but it is very unlikely to happen as the NumberFormat.Field instances are singletons, and there is very less possibility of the usage of these new fields in the old code.
      Show
      Since most of the APIs are newly added, the compatibility risk is minimal. However, two new fields are added to the java.text.NumberFormat.Field which is a serializable class and deserializing one of the new fields by an old runtime may throw an exception (as readResolve() throw an exception for unknown names), but it is very unlikely to happen as the NumberFormat.Field instances are singletons, and there is very less possibility of the usage of these new fields in the old code.
    • Java API
    • SE

      Summary

      Adding support for the compact/short number formatting in JDK.

      Problem

      The existing NumberFormat APIs provide support for formatting/parsing general purpose numbers e.g. decimal, currency, and percentage, but the support for formatting in compact forms of numbers is not available.

      Solution

      Add the support for formatting the numbers in compact forms. Each locale has different compact forms for representing a number, hence a number can be formatted in multiple ways across locales.

      For Example:

      • 1000 can be formatted as "1K", and 1000000 as 1M in "en_US" locale
      • 1000 can be formatted as "1 हज़ार", and 50000000 as "5 क." In "hi_IN" locale

      CLDR provides patterns for compact number formatting. These resources can be utilized to add locale specific compact number formatting support.

      Following major public APIs are introduced for compact number formatting feature:

      CompactNumberFormat class targeted for compact number formatting

      public class CompactNumberFormat extends NumberFormat

      NumberFormat.Style enum for specifying the format style

      public static enum NumberFormat.Style

      Addition of two fields in "java.text.NumberFormat.Field" which can be used to track the position of prefix and suffix in the resulting output.

      public static final NumberFormat.Field PREFIX
      public static final NumberFormat.Field SUFFIX

      public factory methods in NumberFormat.java to obtain CompactNumberFormat instance

      public static NumberFormat getCompactNumberInstance()
      public static NumberFormat getCompactNumberInstance(Locale locale,
                                                   NumberFormat.Style formatStyle)

      Provider method in java/text/spi/NumberFormatProvider

      public NumberFormat getCompactNumberInstance(Locale locale, NumberFormat.Style formatStyle)

      Specification

      http://cr.openjdk.java.net/~arapte/nishjain/8177552/specdiff_cnf.21/overview-summary.html

      Serialized form

      http://cr.openjdk.java.net/~arapte/nishjain/8177552/specdiff_cnf.21/serialized-form.html#java.text.CompactNumberFormat

            nishjain Nishit Jain
            naoto Naoto Sato
            Naoto Sato, Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: