亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

更多QQ空间微信QQ好友腾讯朋友复制链接
您的位置:首頁/技術文章
文章詳情頁

JDK12的新特性之CompactNumberFormat詳解

【字号: 作者:豬豬瀏覽:119日期:2024-07-21 11:15:11

簡介

JDK12引入了新的格式化數字的類叫做CompactNumberFormat。主要方便我們對很長的數字進行簡寫。比如1000可以簡寫為1K或者1 thousand。

本文將會講解CompactNumberFormat的基本構成和使用方法,最后在實際的例子中結束文章的講解。

更多內容請訪問www.flydean.com

CompactNumberFormat詳解

CompactNumberFormat做為格式化數字的一部分是NumberFormat的子類。作用就是將數字進行格式化。要想構建一個CompactNumberFormat,最簡單的辦法就是使用NumberFormat.getCompactNumberInstance方法了。

下面是該方法的定義:

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

方法需要傳入兩個參數:Locale和Style。

Locale

Locale代表著本地語言特性,比如在US locale中,10000可以表示為“10K”,而在China locale中,10000中就變成了“1萬”。

Style

Style有兩種類型,short和long。比如說10000的short表示是“10K”,而它的long表示是“10 thousand”。

JDK已經為我們自定義了很多種內置的Compact實現,我們可以直接使用:

@Test public void testCompactNumberFormat(){ NumberFormat fmtShort = NumberFormat.getCompactNumberInstance( Locale.US, NumberFormat.Style.SHORT); NumberFormat fmtLong = NumberFormat.getCompactNumberInstance( Locale.US, NumberFormat.Style.LONG); log.info(fmtShort.format(312)); log.info(fmtShort.format(3123)); log.info(fmtShort.format(31234)); log.info(fmtLong.format(312)); log.info(fmtLong.format(3123)); log.info(fmtLong.format(31234)); }

輸出結果:

312 3K 31K 312 3 thousand 31 thousand

自定義CompactNumberFormat

除了使用NumberFormat工具類之外,我們還可以自定義CompactNumberFormat。

先看下CompactNumberFormat的定義:

public CompactNumberFormat(String decimalPattern,DecimalFormatSymbols symbols, String[] compactPatterns)public CompactNumberFormat(String decimalPattern,DecimalFormatSymbols symbols, String[] compactPatterns,String pluralRules)

CompactNumberFormat可以接受3個或者4個參數的構造函數。

其中decimalPattern和symbols是用來正常解析數字的,compactPatterns則是用來生成縮寫。pluralRules表示的是復數規則。

@Test public void useCustom(){ String[] compactPatterns = {'', '', '', '0千', '0萬', '00萬', '0百萬', '0千萬', '0億', '00億', '0百億', '0千億', '0兆', '00兆', '000兆'}; DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(Locale.CHINA); CompactNumberFormat format = new CompactNumberFormat( decimalFormat.toPattern(), decimalFormat.getDecimalFormatSymbols(), compactPatterns); log.info(format.format(312340000)); }

上面是一個我們自定義的縮寫規則。

輸出結果:

3億

解析CompactNumber

能生成自然也能夠解析,我們看一個解析的例子:

@Test public void testParse() throws ParseException { NumberFormat fmtLong = NumberFormat.getCompactNumberInstance( Locale.US, NumberFormat.Style.LONG); log.info(String.valueOf(fmtLong.parse('3 thousand'))); }

輸出結果:

3000

總結

本文介紹了JDK12中引入的新的CompactNumberFormat類,希望大家能夠喜歡。

本文的例子

[https://github.com/ddean2009/ learn-java-base-9-to-20](https://github.com/ddean2009/learn-java-base-9-to-20)

到此這篇關于JDK12的新特性:CompactNumberFormat的文章就介紹到這了,更多相關JDK12的新特性:CompactNumberFormat內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: JDK12
主站蜘蛛池模板: 欧美激情一区二区三区蜜桃视频 | 欧美成人aa久久狼窝动画 | 亚洲高清heyzo加勒比 | 成人福利在线播放 | 国产自产v一区二区三区c | 国产精品日日摸夜夜添夜夜添1 | 中文字幕亚洲一区二区v@在线 | 午夜精品福利在线 | 精品久久香蕉国产线看观看亚洲 | 91精品国产综合久久精品 | 黄色网址在线免费观看 | 大学生久久香蕉国产线看观看 | 久久草在线视频免费 | 国产特级 | 久久久精品久久久久久久久久久 | 成人精品人成网站 | 日韩在线你懂的 | 自拍网在线| 青青草国产精品久久久久 | 亚洲一区欧美一区 | 久久99综合国产精品亚洲首页 | 国内精品一级毛片免费看 | 亚洲丝袜在线播放 | 狠狠色丁香婷婷久久综合考虑 | 一本大道一卡2卡三卡4卡麻豆 | 尤物网站在线播放 | 国内欧美一区二区三区 | 国产成人久久综合二区 | 一级黄色录像放 | 亚洲尹人香蕉网在线视颅 | 国产网站在线 | 丝袜美腿国产精品视频一区 | 午夜精品aaa国产福利 | 亚洲国产另类久久久精品小说 | 久久久久久久久免费视频 | 国产探花视频在线观看 | 香蕉国产在线观看免费 | 亚洲一区二区三区免费看 | 欧美一级做一级做片性十三 | 绝对真实偷拍盗摄高清在线视频 | 成 人免费va视频 |