java 格式化輸出數(shù)字的方法
主要使用的類(lèi):java.text.DecimalFormat
1。實(shí)例化對(duì)象,可以用如下兩種方法:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
因?yàn)镈ecimalFormat繼承自NumberFormat。
2。設(shè)定小數(shù)位數(shù)
系統(tǒng)默認(rèn)小數(shù)位數(shù)為3,如:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
System.out.println(df.format(12.3456789));
輸出:12.346
現(xiàn)在可以通過(guò)如下方法把小數(shù)為設(shè)為兩位:
df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789));
則輸出為:12.35
3。將數(shù)字轉(zhuǎn)化為百分比輸出,有如下兩種方法:
(1)
df.applyPattern("##.##%");
System.out.println(df.format(12.3456789));
System.out.println(df.format(1));
System.out.println(df.format(0.015));
輸出分別為:1234.57% 100% 1.5%
(2)
df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789*100)+"%");
System.out.println(df.format(1*100)+"%");
System.out.println(df.format(0.015*100)+"%");
輸出分別為:
1,234.57% 100% 1.5%
4。設(shè)置分組大小
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
System.out.println(df1.format(123456789));
輸出:1,23,45,67,89
還可以通過(guò)df1.setGroupingUsed(false);來(lái)禁用分組設(shè)置,如:
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
df1.setGroupingUsed(false);
System.out.println(df1.format(123456789));
輸出:123456789
5。設(shè)置小數(shù)為必須為2位
DecimalFormat df2=(DecimalFormat) DecimalFormat.getInstance();
df2.applyPattern("0.00");
System.out.println(df2.format(1.2));
輸出:1.20
有時(shí)我們需要控制輸出的數(shù)字的格式,如何使用java的類(lèi)庫(kù)做到這個(gè)呢?
也許你不關(guān)心格式,但是你需要關(guān)心你的程序可以在全世界通用,像下面的這樣一個(gè)簡(jiǎn)單的語(yǔ)句是依賴(lài)地區(qū)的:
System.out.println(1234.56);
在美國(guó),"." 是小數(shù)點(diǎn),但在其它地方就不一定了。如何處理這個(gè)呢?
java.text 包中的一些包可以處理這類(lèi)問(wèn)題。下面的簡(jiǎn)單范例使用那些類(lèi)解決上面提出的問(wèn)題:
import java.text.NumberFormat;
import java.util.Locale;
public class DecimalFormat1 {
public static void main(String args[]) {
// 得到本地的缺省格式
NumberFormat nf1 = NumberFormat.getInstance();
System.out.println(nf1.format(1234.56));
// 得到德國(guó)的格式
NumberFormat nf2 =
NumberFormat.getInstance(Locale.GERMAN);
System.out.println(nf2.format(1234.56));
} }
如果你在美國(guó),運(yùn)行程序后輸出:
1,234.56
1.234,56
換句話(huà)說(shuō),在不同的地方使用不同的習(xí)慣表示數(shù)字。
NumberFormat.getInstance()方法返回NumberFormat的一個(gè)實(shí)例(實(shí)際上是NumberFormat具體的一個(gè)子類(lèi),例如DecimalFormat), 這適合根據(jù)本地設(shè)置格式化一個(gè)數(shù)字。你也可以使用非缺省的地區(qū)設(shè)置,例如德國(guó)。然后格式化方法根據(jù)特定的地區(qū)規(guī)則格式化數(shù)字。這個(gè)程序也可以使用一個(gè)簡(jiǎn)單的形式:
NumberFormat.getInstance().format(1234.56)
但是保存一個(gè)格式然后重用更加有效。國(guó)際化是格式化數(shù)字時(shí)的一個(gè)大問(wèn)題。
另一個(gè)是對(duì)格式的有效控制,例如指定小數(shù)部分的位數(shù),下面是解決這個(gè)問(wèn)題的一個(gè)簡(jiǎn)單例子:
import java.text.DecimalFormat;
import java.util.Locale;
public class DecimalFormat2 {
public static void main(String args[]) {
// 得到本地的缺省格式
DecimalFormat df1 = new DecimalFormat("####.000");
System.out.println(df1.format(1234.56));
// 得到德國(guó)的格式
Locale.setDefault(Locale.GERMAN);
DecimalFormat df2 = new DecimalFormat("####.000");
System.out.println(df2.format(1234.56));
}
}
在這個(gè)例子中設(shè)置了數(shù)字的格式,使用像"####.000"的符號(hào)。這個(gè)模式意味著在小數(shù)點(diǎn)前有四個(gè)數(shù)字,如果不夠就空著,小數(shù)點(diǎn)后有三位數(shù)字,不足用0補(bǔ)齊。程序的輸出:
1234.560
1234,560
相似的,也可以控制指數(shù)形式的格式,例如:
import java.text.DecimalFormat;
public class DecimalFormat3 {
public static void main(String args[]) {
DecimalFormat df = new DecimalFormat("0.000E0000");
System.out.println(df.format(1234.56));
}
}
輸出:
1.235E0003
對(duì)于百分?jǐn)?shù):
import java.text.NumberFormat;
public class DecimalFormat4 {
public static void main(String args[]) {
NumberFormat nf = NumberFormat.getPercentInstance();
System.out.println(nf.format(0.47));
}
}
輸出:
47%
至此,你已經(jīng)看到了格式化數(shù)字的幾個(gè)不同的技術(shù)。另一方面,如何讀取并解析包含格式化的數(shù)字的字符串?解析支持包含在NumberFormat中。例如:
import java.util.Locale;
import java.text.NumberFormat;
import java.text.ParseException;
public class DecimalFormat5 {
public static void main(String args[]) {
// 本地格式
NumberFormat nf1 = NumberFormat.getInstance();
Object obj1 = null;
// 基于格式的解析
try {
obj1 = nf1.parse("1234,56");
}
catch (ParseException e1) {
System.err.println(e1);
}
System.out.println(obj1);
// 德國(guó)格式
NumberFormat nf2 =
NumberFormat.getInstance(Locale.GERMAN);
Object obj2 = null;
// 基于格式的解析
try {
obj2 = nf2.parse("1234,56");
}
catch (ParseException e2) {
System.err.println(e2);
}
System.out.println(obj2);
}
}
這個(gè)例子分兩部分,都是解析一個(gè)字符串:"1234,56"。第一部分使用本地格式解析,第二部分使用德國(guó)格式解析。當(dāng)程序在美國(guó)運(yùn)行,結(jié)果是:
123456
1234.56
換句話(huà)說(shuō),"1234,56"在美國(guó)被認(rèn)為是一個(gè)巨大的整數(shù)123456,而在德國(guó)被認(rèn)為是一個(gè)小數(shù)"1234.56"。
還有格式化討論的最后一個(gè)問(wèn)題。在上面的例子中, DecimalFormat 和 NumberFormat 都被使用了。DecimalFormat 常用于獲得很好的格式控制,而NumberFormat 常用于指定不同于本地的地區(qū)。如何結(jié)合兩個(gè)類(lèi)呢?
答案圍繞著這樣的事實(shí):DecimalFormat是NumberFormat的一個(gè)子類(lèi),其實(shí)例被指定為特定的地區(qū)。因此,你可以使用NumberFormat.getInstance 指定一個(gè)地區(qū),然后將結(jié)構(gòu)強(qiáng)制轉(zhuǎn)換為一個(gè)DecimalFormat對(duì)象。文檔中提到這個(gè)技術(shù)可以在大多情況下適用,但是你需要用try/catch 塊包圍強(qiáng)制轉(zhuǎn)換以防轉(zhuǎn)換不能正常工作 (大概在非常不明顯得情況下使用一個(gè)奇異的地區(qū))。下面是一個(gè)這樣的例子:
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class DecimalFormat6 {
public static void main(String args[]) {
DecimalFormat df = null;
// 得到一個(gè)NumberFormat 對(duì)象并
// 強(qiáng)制轉(zhuǎn)換為一個(gè) DecimalFormat 對(duì)象
try {
df = (DecimalFormat)
NumberFormat.getInstance(Locale.GERMAN);
}
catch (ClassCastException e) {
System.err.println(e);
}
// 設(shè)置格式模式
df.applyPattern("####.00000");
// format a number
System.out.println(df.format(1234.56));
}
}
getInstance() 方法獲得格式,然后調(diào)用applyPattern()方法設(shè)置格式模式,輸出:
1234,56000
如果你不關(guān)心國(guó)際化,可以直接使用DecimalFormat 。
相關(guān)文章
解決RedisTemplate存儲(chǔ)至緩存數(shù)據(jù)出現(xiàn)亂碼的情況
這篇文章主要介紹了解決RedisTemplate存儲(chǔ)至緩存數(shù)據(jù)出現(xiàn)亂碼的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03SpringBoot處理JSON數(shù)據(jù)方法詳解
這篇文章主要介紹了SpringBoot整合Web開(kāi)發(fā)中Json數(shù)據(jù)處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-10-10Java字符串拼接+和StringBuilder的比較與選擇
Java 提供了兩種主要的方式:使用 "+" 運(yùn)算符和使用 StringBuilder 類(lèi),本文主要介紹了Java字符串拼接+和StringBuilder的比較與選擇,感興趣的可以了解一下2023-10-10spring boot項(xiàng)目fat jar瘦身的實(shí)現(xiàn)
這篇文章主要介紹了spring boot項(xiàng)目fat jar瘦身的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Springboot maven plugin插件原理及作用
這篇文章主要介紹了Springboot maven plugin插件原理及作用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10SpringBoot3中token攔截器鏈的設(shè)計(jì)與實(shí)現(xiàn)步驟
本文介紹了spring boot后端服務(wù)開(kāi)發(fā)中有關(guān)如何設(shè)計(jì)攔截器的思路,文中通過(guò)代碼示例和圖文講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03