Java数字格式
當(dāng)我看到其他人編寫不必要的Java代碼并且由于缺乏對已經(jīng)提供所需功能的JDK類的了解而編寫了不必要的Java代碼時(shí),我會想到很多次。 這樣的一個(gè)例子是時(shí)間相關(guān)的常量的使用硬編碼值的寫入,如60 , 24 , 1440 ,和86400時(shí)TIMEUNIT提供了更好的,標(biāo)準(zhǔn)化的方法。 在這篇文章中,我看一看一個(gè)類的示例,該示例提供了開發(fā)人員經(jīng)常在其上實(shí)現(xiàn)的功能: NumberFormat 。
NumberFormat類是java.text包的一部分,該包還包括常用的DateFormat和SimpleDateFormat類。 NumberFormat是一個(gè)抽象類(沒有公共構(gòu)造函數(shù)),其后代的實(shí)例是通過具有諸如getInstance() , getCurrencyInstanceInstance()和getPercentInstance()之類的重載靜態(tài)方法獲得的。
貨幣
下一個(gè)代碼清單演示了如何調(diào)用NumberFormat.getCurrencyInstance(Locale)以獲取NumberFormat的實(shí)例,該實(shí)例以貨幣友好格式顯示數(shù)字。
演示NumberFormat的貨幣支持
/*** Demonstrate use of a Currency Instance of NumberFormat.*/ public void demonstrateCurrency() {writeHeaderToStandardOutput("Currency NumberFormat Examples");final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US);out.println("15.5 -> " + currencyFormat.format(15.5));out.println("15.54 -> " + currencyFormat.format(15.54));out.println("15.345 -> " + currencyFormat.format(15.345)); // rounds to two decimal placesprintCurrencyDetails(currencyFormat.getCurrency()); }/*** Print out details of provided instance of Currency.** @param currency Instance of Currency from which details* will be written to standard output.*/ public void printCurrencyDetails(final Currency currency) {out.println("Concurrency: " + currency);out.println("\tISO 4217 Currency Code: " + currency.getCurrencyCode());out.println("\tISO 4217 Numeric Code: " + currency.getNumericCode());out.println("\tCurrency Display Name: " + currency.getDisplayName(Locale.US));out.println("\tCurrency Symbol: " + currency.getSymbol(Locale.US));out.println("\tCurrency Default Fraction Digits: " + currency.getDefaultFractionDigits()); }執(zhí)行以上代碼后,結(jié)果如下所示:
================================================================================== = Currency NumberFormat Examples ================================================================================== 15.5 -> $15.50 15.54 -> $15.54 15.345 -> $15.35 Concurrency: USDISO 4217 Currency Code: USDISO 4217 Numeric Code: 840Currency Display Name: US DollarCurrency Symbol: $Currency Default Fraction Digits: 2上面的代碼和相關(guān)的輸出表明,用于貨幣的NumberFormat實(shí)例(實(shí)際上是DecimalFormat )會根據(jù)語言環(huán)境自動應(yīng)用適當(dāng)?shù)奈粩?shù)和適當(dāng)?shù)呢泿欧枴?
百分比
下一個(gè)代碼清單和相關(guān)的輸出演示了NumberFormat使用,以百分比友好格式顯示數(shù)字。
演示NumberFormat的百分比格式
/*** Demonstrate use of a Percent Instance of NumberFormat.*/ public void demonstratePercentage() {writeHeaderToStandardOutput("Percentage NumberFormat Examples");final NumberFormat percentageFormat = NumberFormat.getPercentInstance(Locale.US);out.println("Instance of: " + percentageFormat.getClass().getCanonicalName());out.println("1 -> " + percentageFormat.format(1));// will be 0 because truncated to Integer by Integer divisionout.println("75/100 -> " + percentageFormat.format(75/100));out.println(".75 -> " + percentageFormat.format(.75));out.println("75.0/100 -> " + percentageFormat.format(75.0/100));// will be 0 because truncated to Integer by Integer divisionout.println("83/93 -> " + percentageFormat.format((83/93)));out.println("93/83 -> " + percentageFormat.format(93/83));out.println(".5 -> " + percentageFormat.format(.5));out.println(".912 -> " + percentageFormat.format(.912));out.println("---- Setting Minimum Fraction Digits to 1:");percentageFormat.setMinimumFractionDigits(1);out.println("1 -> " + percentageFormat.format(1));out.println(".75 -> " + percentageFormat.format(.75));out.println("75.0/100 -> " + percentageFormat.format(75.0/100));out.println(".912 -> " + percentageFormat.format(.912)); }================================================================================== = Percentage NumberFormat Examples ================================================================================== 1 -> 100% 75/100 -> 0% .75 -> 75% 75.0/100 -> 75% 83/93 -> 0% 93/83 -> 100% .5 -> 50% .912 -> 91% ---- Setting Minimum Fraction Digits to 1: 1 -> 100.0% .75 -> 75.0% 75.0/100 -> 75.0% .912 -> 91.2%代碼和百分比的輸出NumberFormat使用表明,通過默認(rèn)的實(shí)例NumberFormat (實(shí)際上是一個(gè)DecimalFormat通過返回在這種情況下) NumberFormat.getPercentInstance(區(qū)域)的方法沒有小數(shù)位,乘所提供的數(shù)目由100(假定它是(如果提供的話,則為百分比的十進(jìn)制等效值 ),并添加一個(gè)百分號(%)。
整數(shù)
接下來顯示的少量代碼及其相關(guān)輸出演示了NumberFormat使用,以整數(shù)格式顯示數(shù)字。
演示NumberFormat的整數(shù)格式
/*** Demonstrate use of an Integer Instance of NumberFormat.*/ public void demonstrateInteger() {writeHeaderToStandardOutput("Integer NumberFormat Examples");final NumberFormat integerFormat = NumberFormat.getIntegerInstance(Locale.US);out.println("7.65 -> " + integerFormat.format(7.65));out.println("7.5 -> " + integerFormat.format(7.5));out.println("7.49 -> " + integerFormat.format(7.49));out.println("-23.23 -> " + integerFormat.format(-23.23)); }================================================================================== = Integer NumberFormat Examples ================================================================================== 7.65 -> 8 7.5 -> 8 7.49 -> 7 -23.23 -> -23如上面的代碼和相關(guān)輸出所示, NumberFormat方法getIntegerInstance(Locale)返回一個(gè)實(shí)例,該實(shí)例將提供的數(shù)字表示為整數(shù)。
固定位數(shù)
下一個(gè)代碼清單和相關(guān)輸出演示了如何使用NumberFormat打印浮點(diǎn)數(shù)的定點(diǎn)表示形式。 換句話說,使用NumberFormat可以使一個(gè)數(shù)字在小數(shù)點(diǎn)左側(cè)(“整數(shù)”數(shù)字)和小數(shù)點(diǎn)右側(cè)(“小數(shù)”數(shù)字)的正好具有規(guī)定位數(shù)的數(shù)字表示。
演示定點(diǎn)數(shù)字的NumberFormat
/*** Demonstrate generic NumberFormat instance with rounding mode,* maximum fraction digits, and minimum integer digits specified.*/ public void demonstrateNumberFormat() {writeHeaderToStandardOutput("NumberFormat Fixed-Point Examples");final NumberFormat numberFormat = NumberFormat.getNumberInstance();numberFormat.setRoundingMode(RoundingMode.HALF_UP);numberFormat.setMaximumFractionDigits(2);numberFormat.setMinimumIntegerDigits(1);out.println(numberFormat.format(234.234567));out.println(numberFormat.format(1));out.println(numberFormat.format(.234567));out.println(numberFormat.format(.349));out.println(numberFormat.format(.3499));out.println(numberFormat.format(0.9999)); }================================================================================== = NumberFormat Fixed-Point Examples ================================================================================== 234.23 1 0.23 0.34 0.35 1上面的代碼和相關(guān)的輸出演示了對最小“整數(shù)”位數(shù)的精確控制,該位數(shù)代表小數(shù)點(diǎn)左邊(至少一個(gè),因此在適用時(shí)顯示為零)和最大“分?jǐn)?shù)”小數(shù)點(diǎn)右邊的數(shù)字。 盡管未顯示,但也可以指定最大整數(shù)位數(shù)和最小分?jǐn)?shù)位數(shù)。
結(jié)論
我曾經(jīng)用過這篇文章來研究如何使用NumberFormat以不同的方式顯示數(shù)字(貨幣,百分比,整數(shù),固定的小數(shù)點(diǎn)等),并且通常意味著無需編寫或減少代碼即可將數(shù)字寫成數(shù)字格式。 當(dāng)我第一次開始寫這篇文章時(shí),我設(shè)想包括有關(guān)NumberFormat的直接后代( DecimalFormat和ChoiceFormat )的示例和討論,但是已經(jīng)確定這篇文章已經(jīng)足夠冗長了。 我可能會在以后的博客文章中介紹NumberFormat這些后代。
翻譯自: https://www.javacodegeeks.com/2014/08/java-numeric-formatting.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
- 上一篇: 安卓电视棒刷机(安卓电视棒)
- 下一篇: Java验证(javafx)