JDK12的新特性之CompactNumberFormat詳解
簡介
JDK12引入了新的格式化數(shù)字的類叫做CompactNumberFormat。主要方便我們對很長的數(shù)字進(jìn)行簡寫。比如1000可以簡寫為1K或者1 thousand。
本文將會講解CompactNumberFormat的基本構(gòu)成和使用方法,最后在實(shí)際的例子中結(jié)束文章的講解。
更多內(nèi)容請?jiān)L問www.flydean.com
CompactNumberFormat詳解
CompactNumberFormat做為格式化數(shù)字的一部分是NumberFormat的子類。作用就是將數(shù)字進(jìn)行格式化。要想構(gòu)建一個CompactNumberFormat,最簡單的辦法就是使用NumberFormat.getCompactNumberInstance方法了。
下面是該方法的定義:
public static NumberFormat getCompactNumberInstance(Locale locale, NumberFormat.Style formatStyle)
方法需要傳入兩個參數(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),我們可以直接使用:
@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));
}
輸出結(jié)果:
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個參數(shù)的構(gòu)造函數(shù)。
其中decimalPattern和symbols是用來正常解析數(shù)字的,compactPatterns則是用來生成縮寫。pluralRules表示的是復(fù)數(shù)規(guī)則。
@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));
}
上面是一個我們自定義的縮寫規(guī)則。
輸出結(jié)果:
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")));
}
輸出結(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)
到此這篇關(guān)于JDK12的新特性:CompactNumberFormat的文章就介紹到這了,更多相關(guān)JDK12的新特性:CompactNumberFormat內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot2深入分析解決java.lang.ArrayStoreException異常
這篇文章介紹了Spring Boot2深入分析解決java.lang.ArrayStoreException異常的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
深入了解Java中的過濾器Filter和監(jiān)聽器Listener
這篇文章主要為大家詳細(xì)介紹了Java中的過濾器Filter和監(jiān)聽器Listener的使用以及二者的區(qū)別,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06
MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解
這篇文章主要介紹了MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Spring?Boot?接口加解密功能實(shí)現(xiàn)
在我們?nèi)粘5腏ava開發(fā)中,免不了和其他系統(tǒng)的業(yè)務(wù)交互,或者微服務(wù)之間的接口調(diào)用;如果我們想保證數(shù)據(jù)傳輸?shù)陌踩?,對接口出參加密,入?yún)⒔饷?,這篇文章主要介紹了Spring?Boot?接口加解密功能實(shí)現(xiàn),需要的朋友可以參考下2023-04-04
Java實(shí)現(xiàn)samza轉(zhuǎn)換成flink
將Apache Samza作業(yè)遷移到Apache Flink作業(yè)是一個復(fù)雜的任務(wù),因?yàn)檫@兩個流處理框架有不同的API和架構(gòu),本文我們就來看看如何使用Java實(shí)現(xiàn)samza轉(zhuǎn)換成flink吧2024-11-11
Java實(shí)現(xiàn)FTP文件與文件夾的上傳和下載
本文主要分享了Java實(shí)現(xiàn)文件上傳和下載的具體實(shí)例,分為單個文件的上傳與下載和整個文件夾的上傳與下載。具有很好的參考價值,需要的朋友一起來看下吧2016-12-12
java使用poi讀取excel內(nèi)容方法實(shí)例
本文介紹java使用poi讀取excel內(nèi)容的實(shí)例,大家參考使用吧2014-01-01

