Using Minimum Fractional Digits With JDK 12 Compact Number Formatting
Learn more about the NumberFormat class and fractional digits in JDK 12
Join the DZone community and get the full member experience.
Join For FreeThe post "Compact Number Formatting Comes to JDK 12" demonstrated the support added to NumberFormat
in JDK 12 to support compact number formatting. The examples shown in that post only used the instances of NumberFormat
returned by invocations of NumberFormat
's new overloaded getCompactNumberInstance(-)
methods, and so, therefore, they did not specify characteristics such as minimum fractional digits and maximum fractional digits. The results, in some cases, are less than desirable. Fortunately, NumberFormat
does allow for minimum and maximum fractional digits to be specified, and this post demonstrates how that can improve the output of the compact number formatting available with JDK 12.
The code listing introduced in the original "Compact Number Formatting Comes to JDK 12" post (which is available on GitHub) has been updated to demonstrate the use of NumberFormat.setMinimumFractionDigits(int). An excerpt of that code is shown next and is followed by the accompanying output.
/**
* Generates standardized map of labels to Compact Number Format
* instances described by the labels. The instances of {@code NumberFormat}
* are created with Locale and Style only and with the provided number
* of minimum fractional digits.
*
* @return Mapping of label to an instance of a Compact Number Format
* consisting of a Locale, Style, and specified minimum number of fractional
* digits that is described by the label.
*/
private static Map<String, NumberFormat> generateCompactNumberFormats(
final int minimumNumberFractionDigits)
{
var numberFormats = generateCompactNumberFormats();
numberFormats.forEach((label, numberFormat) ->
numberFormat.setMinimumFractionDigits(minimumNumberFractionDigits));
return numberFormats;
}
/**
* Demonstrates compact number formatting in a variety of locales
* and number formats against the provided {@code long} value and
* with a minimum fractional digits of 1 specified.
* @param numberToFormat Value of type {@code long} that is to be
* formatted using compact number formatting and a variety of
* locales and number formats and with a single minimal fractional
* digit.
*/
private static void demonstrateCompactNumberFormattingOneFractionalDigitMinimum(
final long numberToFormat)
{
final Map<String, NumberFormat> numberFormats = generateCompactNumberFormats(1);
out.println(
"Demonstrating Compact Number Formatting on long '" + numberToFormat
+ "' with 1 minimum fraction digit:");
numberFormats.forEach((label, numberFormat) ->
out.println("\t" + label + ": " + numberFormat.format(numberToFormat))
);
}
Demonstrating Compact Number Formatting on long '15' with 1 minimum fraction digit:
Default: 15
US/Long: 15
UK/Short: 15
UK/Long: 15
FR/Short: 15
FR/Long: 15
DE/Short: 15
DE/Long: 15
IT/Short: 15
IT/Long: 15
Demonstrating Compact Number Formatting on long '150' with 1 minimum fraction digit:
Default: 150
US/Long: 150
UK/Short: 150
UK/Long: 150
FR/Short: 150
FR/Long: 150
DE/Short: 150
DE/Long: 150
IT/Short: 150
IT/Long: 150
Demonstrating Compact Number Formatting on long '1500' with 1 minimum fraction digit:
Default: 1.5K
US/Long: 1.5 thousand
UK/Short: 1.5K
UK/Long: 1.5 thousand
FR/Short: 1,5 k
FR/Long: 1,5 millier
DE/Short: 1.500
DE/Long: 1,5 Tausend
IT/Short: 1.500
IT/Long: 1,5 mille
Demonstrating Compact Number Formatting on long '15000' with 1 minimum fraction digit:
Default: 15.0K
US/Long: 15.0 thousand
UK/Short: 15.0K
UK/Long: 15.0 thousand
FR/Short: 15,0 k
FR/Long: 15,0 mille
DE/Short: 15.000
DE/Long: 15,0 Tausend
IT/Short: 15.000
IT/Long: 15,0 mila
Demonstrating Compact Number Formatting on long '150000' with 1 minimum fraction digit:
Default: 150.0K
US/Long: 150.0 thousand
UK/Short: 150.0K
UK/Long: 150.0 thousand
FR/Short: 150,0 k
FR/Long: 150,0 mille
DE/Short: 150.000
DE/Long: 150,0 Tausend
IT/Short: 150.000
IT/Long: 150,0 mila
Demonstrating Compact Number Formatting on long '1500000' with 1 minimum fraction digit:
Default: 1.5M
US/Long: 1.5 million
UK/Short: 1.5M
UK/Long: 1.5 million
FR/Short: 1,5 M
FR/Long: 1,5 million
DE/Short: 1,5 Mio.
DE/Long: 1,5 Million
IT/Short: 1,5 Mln
IT/Long: 1,5 milione
Demonstrating Compact Number Formatting on long '15000000' with 1 minimum fraction digit:
Default: 15.0M
US/Long: 15.0 million
UK/Short: 15.0M
UK/Long: 15.0 million
FR/Short: 15,0 M
FR/Long: 15,0 million
DE/Short: 15,0 Mio.
DE/Long: 15,0 Millionen
IT/Short: 15,0 Mln
IT/Long: 15,0 milioni
As the example and output shown above demonstrate, the use of the NumberFormat.setMinimumFractionDigits(int)
leads to compact number formatted output that is likely to be more aesthetically pleasing in many cases. There is a recent discussion "Compact Number Formatting and Fraction Digits" on the OpenJDK core-libs-dev mailing list that also discusses this ability to customize the compact number formatting output.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments