jdk安装教程
帖子“紧凑数字格式出现在JDK 12中”演示了对JDK 12中NumberFormat
的支持,以支持紧凑数字格式。 该帖子中显示的示例仅使用NumberFormat
的实例,这些实例是通过调用NumberFormat
的新重载getCompactNumberInstance(-)
方法返回的,因此没有指定诸如最小小数位和最大小数位之类的特性。 在某些情况下,结果不理想。 幸运的是, NumberFormat
确实允许指定最小和最大小数位,并且本文演示了如何改善JDK 12提供的紧凑数字格式的输出。
原始“紧凑型数字格式来到JDK 12 ”一文( 可在GitHub上找到)中介绍的代码清单已更新,以演示NumberFormat.setMinimumFractionDigits(int)的使用。 接下来是该代码的摘录,然后是随附的输出。
/**
* 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
如上面的示例和输出所示,使用NumberFormat.setMinimumFractionDigits(int)
会产生紧凑的数字格式输出,在许多情况下,从美学角度来看,这种输出可能会更令人满意。 OpenJDK core-libs-dev邮件列表上最近有一个讨论“紧凑数字格式和小数位数”,还讨论了自定义紧凑数字格式输出的功能。
翻译自: https://www.javacodegeeks.com/2019/01/usingfractional-digits-compact-number.html
jdk安装教程