Java数据保留小数
?
例子:
import java.text.*; public class Untitled1 { public static void main(String[] args) { //--------------------------------------------- //定義一個(gè)數(shù)字格式化對象,格式化模板為".##",即保留2位小數(shù). DecimalFormat a = new DecimalFormat(".##"); String s= a.format(333.335); System.err.println(s); //說明:如果小數(shù)點(diǎn)后面不夠2位小數(shù),不會(huì)補(bǔ)零.參見Rounding小節(jié) //--------------------------------------------- //----------------------------------------------- //可以在運(yùn)行時(shí)刻用函數(shù)applyPattern(String)修改格式模板 //保留2位小數(shù),如果小數(shù)點(diǎn)后面不夠2位小數(shù)會(huì)補(bǔ)零 a.applyPattern(".00"); s = a.format(333.3); System.err.println(s); //------------------------------------------------ //------------------------------------------------ //添加千分號(hào) a.applyPattern(".##\u2030"); s = a.format(0.78934); System.err.println(s);//添加千位符后,小數(shù)會(huì)進(jìn)三位并加上千位符 //------------------------------------------------ //------------------------------------------------ //添加百分號(hào) a.applyPattern("#.##%"); s = a.format(0.78645); System.err.println(s); //------------------------------------------------ //------------------------------------------------ //添加前、后修飾字符串,記得要用單引號(hào)括起來 a.applyPattern("'這是我的錢$',###.###'美圓'"); s = a.format(33333443.3333); System.err.println(s); //------------------------------------------------ //------------------------------------------------ //添加貨幣表示符號(hào)(不同的國家,添加的符號(hào)不一樣 a.applyPattern("\u00A4"); s = a.format(34); System.err.println(s); //------------------------------------------------ //----------------------------------------------- //定義正負(fù)數(shù)模板,記得要用分號(hào)隔開 a.applyPattern("0.0;'@'-#.0"); s = a.format(33); System.err.println(s); s = a.format(-33); System.err.println(s); //----------------------------------------------- //綜合運(yùn)用,正負(fù)數(shù)的不同前后綴 String pattern="'my moneny'###,###.##'RMB';'ur money'###,###.##'US'"; a.applyPattern(pattern); System.out.println(a.format(1223233.456)); } }
?
總結(jié):
要生成一個(gè)DecimalFormat對象,一般只要通過NumberFormat類工廠的getInstance()來取得一個(gè)NumberFormat對象再將其轉(zhuǎn)換成DecimalFormat對象,然后通過DecimalForat對象的applyPattern()來動(dòng)態(tài)改變數(shù)據(jù)的現(xiàn)示格式模板,通過format()方法取得格式化后的數(shù)字。同時(shí),DecimalFormat提供了許多的方法來返回格式化后的數(shù)字的某一部份,這些方法如:getNegativeSuffix()。這個(gè)類的難點(diǎn)主要是在模板的書寫及理解上。其實(shí)主要的就是針對一個(gè)數(shù)字的正負(fù)形式來設(shè)定不同的格式顯示。這里要特別注意的是使用在模板上的特殊字符代表有特殊的意義,如下表所示:
Symbol??? Description
0??? a digit
#??? a digit, zero shows as absent
.??? placeholder for decimal separator
,??? placeholder for grouping separator
E?? separates mantissa and exponent for exponential formats
;??? separates formats
-??? default negative prefix
%??? multiply by 100 and show as percentage
???? multiply by 1000 and show as per mille
¤??? currency sign; replaced by currency symbol; if doubled, replaced by international currency symbol; if present in a pattern, the monetary decimal separator is used instead of the decimal separator?
X??? any other characters can be used in the prefix or suffix
'??? used to quote special characters in a prefix or suffix
例如:如果模板中含有#,意思是指這個(gè)#號(hào)可代表一個(gè)或多個(gè)數(shù)字如果該位的數(shù)字是零的話則省略該位。另:注意“#,##0.0#;(#)”這個(gè)模板的意思是指數(shù)字的負(fù)數(shù)形式跟正數(shù)的一樣。
總結(jié)
以上是生活随笔為你收集整理的Java数据保留小数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSP字体设置
- 下一篇: Java 字符串 String 与整数型