JDK12的新特性:CompactNumberFormat
文章目錄
- 簡(jiǎn)介
- CompactNumberFormat詳解
- 自定義CompactNumberFormat
- 解析CompactNumber
- 總結(jié)
JDK12的新特性:CompactNumberFormat
簡(jiǎn)介
JDK12引入了新的格式化數(shù)字的類叫做CompactNumberFormat。主要方便我們對(duì)很長(zhǎng)的數(shù)字進(jìn)行簡(jiǎn)寫。比如1000可以簡(jiǎn)寫為1K或者1 thousand。
本文將會(huì)講解CompactNumberFormat的基本構(gòu)成和使用方法,最后在實(shí)際的例子中結(jié)束文章的講解。
更多內(nèi)容請(qǐng)?jiān)L問www.flydean.com
喜歡的小伙伴可以點(diǎn)個(gè)關(guān)注謝謝!
CompactNumberFormat詳解
CompactNumberFormat做為格式化數(shù)字的一部分是NumberFormat的子類。作用就是將數(shù)字進(jìn)行格式化。要想構(gòu)建一個(gè)CompactNumberFormat,最簡(jiǎn)單的辦法就是使用NumberFormat.getCompactNumberInstance方法了。
下面是該方法的定義:
public static NumberFormat getCompactNumberInstance(Locale locale,NumberFormat.Style formatStyle)方法需要傳入兩個(gè)參數(shù):Locale和Style。
Locale
Locale代表著本地語言特性,比如在US locale中,10000可以表示為“10K”,而在China locale中,10000中就變成了“1萬”。
Style
Style有兩種類型,short和long。比如說10000的short表示是“10K”,而它的long表示是“10 thousand”。
JDK已經(jīng)為我們自定義了很多種內(nèi)置的Compact實(shí)現(xiàn),我們可以直接使用:
@Testpublic 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));}輸出結(jié)果:
3123K31K3123 thousand31 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個(gè)或者4個(gè)參數(shù)的構(gòu)造函數(shù)。
其中decimalPattern和symbols是用來正常解析數(shù)字的,compactPatterns則是用來生成縮寫。pluralRules表示的是復(fù)數(shù)規(guī)則。
@Testpublic 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));}上面是一個(gè)我們自定義的縮寫規(guī)則。
輸出結(jié)果:
3億解析CompactNumber
能生成自然也能夠解析,我們看一個(gè)解析的例子:
@Testpublic void testParse() throws ParseException {NumberFormat fmtLong = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);log.info(String.valueOf(fmtLong.parse("3 thousand")));}輸出結(jié)果:
3000總結(jié)
本文介紹了JDK12中引入的新的CompactNumberFormat類,希望大家能夠喜歡。
本文的例子[https://github.com/ddean2009/
learn-java-base-9-to-20](https://github.com/ddean2009/
learn-java-base-9-to-20)
更多精彩內(nèi)容且看:
- 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級(jí)賬本,以太坊,Libra,比特幣等持續(xù)更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
- Spring 5.X系列教程:滿足你對(duì)Spring5的一切想象-持續(xù)更新
- java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程
本文作者:flydean程序那些事
本文鏈接:http://www.flydean.com/jdk12-compactnumberformat/
本文來源:flydean的博客
歡迎關(guān)注我的公眾號(hào):程序那些事,更多精彩等著您!
總結(jié)
以上是生活随笔為你收集整理的JDK12的新特性:CompactNumberFormat的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDK12的新特性:teeing col
- 下一篇: JDK11的新特性:新的HTTP API