java 使用DecimalFormat進(jìn)行數(shù)字的格式化實例詳解
更新時間:2017年06月14日 09:58:16 作者:elim168
這篇文章主要介紹了java 使用DecimalFormat進(jìn)行數(shù)字的格式化實例詳解的相關(guān)資料,需要的朋友可以參考下
java 使用DecimalFormat進(jìn)行數(shù)字的格式化實例詳解
簡單實例:
//獲取DecimalFormat的方法DecimalFormat.getInstance(); public static void test1(DecimalFormat df) { //默認(rèn)顯示3位小數(shù) double d = 1.5555555; System.out.println(df.format(d));//1.556 //設(shè)置小數(shù)點后最大位數(shù)為5 df.setMaximumFractionDigits(5); df.setMinimumIntegerDigits(15); System.out.println(df.format(d));//1.55556 df.setMaximumFractionDigits(2); System.out.println(df.format(d));//1.56 //設(shè)置小數(shù)點后最小位數(shù),不夠的時候補(bǔ)0 df.setMinimumFractionDigits(10); System.out.println(df.format(d));//1.5555555500 //設(shè)置整數(shù)部分最小長度為3,不夠的時候補(bǔ)0 df.setMinimumIntegerDigits(3); System.out.println(df.format(d)); //設(shè)置整數(shù)部分的最大值為2,當(dāng)超過的時候會從個位數(shù)開始取相應(yīng)的位數(shù) df.setMaximumIntegerDigits(2); System.out.println(df.format(d)); } public static void test2(DecimalFormat df) { int number = 155566; //默認(rèn)整數(shù)部分三個一組, System.out.println(number);//輸出格式155,566 //設(shè)置每四個一組 df.setGroupingSize(4); System.out.println(df.format(number));//輸出格式為15,5566 DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(); //設(shè)置小數(shù)點分隔符 dfs.setDecimalSeparator(';'); //設(shè)置分組分隔符 dfs.setGroupingSeparator('a'); df.setDecimalFormatSymbols(dfs); System.out.println(df.format(number));//15a5566 System.out.println(df.format(11.22));//11;22 //取消分組 df.setGroupingUsed(false); System.out.println(df.format(number)); } public static void test3(DecimalFormat df) { double a = 1.220; double b = 11.22; double c = 0.22; //占位符可以使用0和#兩種,當(dāng)使用0的時候會嚴(yán)格按照樣式來進(jìn)行匹配,不夠的時候會補(bǔ)0,而使用#時會將前后的0進(jìn)行忽略 //按百分比進(jìn)行輸出 // df.applyPattern("00.00%"); df.applyPattern("##.##%"); System.out.println(df.format(a));//122% System.out.println(df.format(b));//1122% System.out.println(df.format(c));//22% double d = 1.22222222; //按固定格式進(jìn)行輸出 df.applyPattern("00.000"); System.out.println(df.format(d));//01.222 df.applyPattern("##.###"); System.out.println(df.format(d));//1.222 }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Eclipse中Properties和yml配置文件注釋亂碼的解決
這篇文章主要介紹了Eclipse中Properties和yml配置文件注釋亂碼的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10nacos使用占位符${}進(jìn)行參數(shù)配置的方法
這篇文章主要介紹了nacos如何使用占位符${}進(jìn)行參數(shù)配置,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12SpringBoot中實現(xiàn)訂單30分鐘自動取消的三種方案分享
在電商和其他涉及到在線支付的應(yīng)用中,通常需要實現(xiàn)一個功能:如果用戶在生成訂單后的一定時間內(nèi)未完成支付,系統(tǒng)將自動取消該訂單,本文將詳細(xì)介紹基于Spring Boot框架實現(xiàn)訂單30分鐘內(nèi)未支付自動取消的幾種方案,并提供實例代碼,需要的朋友可以參考下2023-10-10Java實現(xiàn)查找當(dāng)前字符串最大回文串代碼分享
本文給大家介紹的是如何使用Java實現(xiàn)查找當(dāng)前字符串最大回文串代碼,非常的簡單實用,有需要的小伙伴可以參考下2016-07-07