精简jdk包_在JDK 12精简数字格式中使用最小分数数字
精簡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: 15US/Long: 15UK/Short: 15UK/Long: 15FR/Short: 15FR/Long: 15DE/Short: 15DE/Long: 15IT/Short: 15IT/Long: 15 Demonstrating Compact Number Formatting on long '150' with 1 minimum fraction digit:Default: 150US/Long: 150UK/Short: 150UK/Long: 150FR/Short: 150FR/Long: 150DE/Short: 150DE/Long: 150IT/Short: 150IT/Long: 150 Demonstrating Compact Number Formatting on long '1500' with 1 minimum fraction digit:Default: 1.5KUS/Long: 1.5 thousandUK/Short: 1.5KUK/Long: 1.5 thousandFR/Short: 1,5?kFR/Long: 1,5 millierDE/Short: 1.500DE/Long: 1,5 TausendIT/Short: 1.500IT/Long: 1,5 mille Demonstrating Compact Number Formatting on long '15000' with 1 minimum fraction digit:Default: 15.0KUS/Long: 15.0 thousandUK/Short: 15.0KUK/Long: 15.0 thousandFR/Short: 15,0?kFR/Long: 15,0 milleDE/Short: 15.000DE/Long: 15,0 TausendIT/Short: 15.000IT/Long: 15,0 mila Demonstrating Compact Number Formatting on long '150000' with 1 minimum fraction digit:Default: 150.0KUS/Long: 150.0 thousandUK/Short: 150.0KUK/Long: 150.0 thousandFR/Short: 150,0?kFR/Long: 150,0 milleDE/Short: 150.000DE/Long: 150,0 TausendIT/Short: 150.000IT/Long: 150,0 mila Demonstrating Compact Number Formatting on long '1500000' with 1 minimum fraction digit:Default: 1.5MUS/Long: 1.5 millionUK/Short: 1.5MUK/Long: 1.5 millionFR/Short: 1,5?MFR/Long: 1,5 millionDE/Short: 1,5?Mio.DE/Long: 1,5 MillionIT/Short: 1,5?MlnIT/Long: 1,5 milione Demonstrating Compact Number Formatting on long '15000000' with 1 minimum fraction digit:Default: 15.0MUS/Long: 15.0 millionUK/Short: 15.0MUK/Long: 15.0 millionFR/Short: 15,0?MFR/Long: 15,0 millionDE/Short: 15,0?Mio.DE/Long: 15,0 MillionenIT/Short: 15,0?MlnIT/Long: 15,0 milioni如上面的示例和輸出所示,使用NumberFormat.setMinimumFractionDigits(int)可以生成緊湊的數字格式輸出,在許多情況下,從美學角度來看,這種輸出可能會更令人滿意。 OpenJDK core-libs-dev郵件列表上最近有一個討論“ 緊湊數字格式和小數位數 ”,該討論還討論了自定義緊湊數字格式輸出的功能。
翻譯自: https://www.javacodegeeks.com/2019/01/usingfractional-digits-compact-number.html
精簡jdk包
總結
以上是生活随笔為你收集整理的精简jdk包_在JDK 12精简数字格式中使用最小分数数字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gradle使用maven_使用Grad
- 下一篇: 装修行业怎么做网络营销(装修行业怎么做网