聊聊DecimalFormat的用法及各符號(hào)的意義
DecimalFormat的用法及各符號(hào)的意義
| 符號(hào) | 位置 | 本地化? | 含義 |
| 0 | 數(shù)字 | 是 | 阿拉伯?dāng)?shù)字 |
| # | 數(shù)字字 | 是 | 阿拉伯?dāng)?shù)字,如果不存在則顯示為空 |
| . | 數(shù)字 | 是 | 小數(shù)分隔符或貨幣小數(shù)分隔符 |
| - | 數(shù)字 | 是 | 減號(hào) |
| , | 數(shù)字 | 是 | 分組分隔符 |
| E | 數(shù)字 | 是 | 分隔科學(xué)計(jì)數(shù)法中的尾數(shù)和指數(shù)。在前綴或后綴中無需加引號(hào)。 |
| ; | 子模式邊界 | 是 | 分隔正數(shù)和負(fù)數(shù)子模式 |
| % | 前綴或后綴 | 是 | 乘以 100 并顯示為百分?jǐn)?shù) |
| /u2030 | 前綴或后綴 | 是 | 乘以 1000 并顯示為千分?jǐn)?shù) |
| ¤(/u00A4) | 前綴或后綴 | 否 | 貨幣記號(hào),由貨幣符號(hào)替換。如果兩個(gè)同時(shí)出現(xiàn),則用國際貨幣符號(hào)替換。如果出現(xiàn)在某個(gè)模式中,則使用貨幣小數(shù)分隔符,而不使用小數(shù)分隔符。 |
| ' | 前綴或后綴 | 否 | 用于在前綴或或后綴中為特殊字符加引號(hào),例如 "'#'#" 將 123 格式化為 "#123"。要?jiǎng)?chuàng)建單引號(hào)本身,請連續(xù)使用兩個(gè)單引號(hào):"# o''clock"。 |
DecimalFormat format = new DecimalFormat("###,####.000");
System.out.println(format.format(111111123456.1227222)); // 1111,1112,3456.123
Locale.setDefault(Locale.US);
DecimalFormat usFormat = new DecimalFormat("###,###.000");
System.out.println(usFormat.format(111111123456.1227222)); // 111,111,123,456.123
DecimalFormat addPattenFormat = new DecimalFormat();
addPattenFormat.applyPattern("##,###.000");
System.out.println(addPattenFormat.format(111111123456.1227)); // 111,111,123,456.123
DecimalFormat zhiFormat = new DecimalFormat();
zhiFormat.applyPattern("0.000E0000");
System.out.println(zhiFormat.format(10000)); // 1.000E0004
System.out.println(zhiFormat.format(12345678.345)); // 1.235E0007
DecimalFormat percentFormat = new DecimalFormat();
percentFormat.applyPattern("#0.000%");
System.out.println(percentFormat.format(0.3052222)); // 30.522%
使用DecimalFormat時(shí)注意事項(xiàng)
import java.text.DecimalFormat;
public class Format {
public static void main(String[] args) {
double ss=12345.97468;
double ss2=0.23;
DecimalFormat ff=new DecimalFormat("#,###,###.######"); 綠色區(qū)域?yàn)楸A舻男?shù)位數(shù)(四舍五入)----但不會(huì)保 留末尾的0) --------------------整數(shù)的話就沒有小數(shù)位
DecimalFormat ff2=new DecimalFormat("#,###,###.0000"); //小數(shù)點(diǎn)后保留四位小數(shù)(保留末尾的0) //類似于 round()函數(shù)---四舍五入
DecimalFormat ff3=new DecimalFormat("#,###,###.00000"); //小數(shù)點(diǎn)后保留五位小數(shù)(保留末尾的0)
DecimalFormat ff4=new DecimalFormat("#,###,###0.0000"); //小數(shù)點(diǎn)后保留四位小數(shù)(保留末尾的0)
DecimalFormat ff5=new DecimalFormat("#,###,###0.00000"); //小數(shù)點(diǎn)后保留五位小數(shù)(保留末尾的0)
System.out.println("#,###,###.######: "+ff.format(ss));
System.out.println("#,###,###.0000 : "+ff2.format(ss)); //類似于round()函數(shù)---四舍五入
System.out.println("#,###,###.00000 : "+ff3.format(ss));
System.out.println("---------------------------------------------------");
System.out.println("f111#,###,###.######: "+ff.format(ss2));
System.out.println("f222#,###,###.0000 : "+ff2.format(ss2));
System.out.println("f333#,###,###.00000 : "+ff3.format(ss2));
System.out.println("---------------------------------------------------");
System.out.println("f111#,###,###.######: "+ff.format(ss2));
System.out.println("f444#,###,###.0000 : "+ff4.format(ss2));
System.out.println("f555#,###,###.00000 : "+ff5.format(ss2));
System.out.println("---------------------------------------------------");
System.out.println("sss#,###,###.0000 : "+ff4.format(ss));
System.out.println("sss#,###,###.00000 : "+ff5.format(ss));
}
}
結(jié)果:
#,###,###.######: 12,345.97468
#,###,###.0000 : 12,345.9747
#,###,###.00000 : 12,345.97468
---------------------------------------------------
f111#,###,###.######: 0.23
f222#,###,###.0000 : .2300 (有問題)
f333#,###,###.00000 : .23000 (有問題)
---------------------------------------------------
f111#,###,###.######: 0.23
f444#,###,###.0000 : 0.2300
f555#,###,###.00000 : 0.23000---------------------------------------------------
sss#,###,###.0000 : 1,2345.9747 (有問題) ---千位符位置有問題
sss#,###,###.00000 : 1,2345.97468 (有問題) ---千位符位置有問題
總結(jié)一下吧
(1)double ss2=0.23;時(shí)使用:
DecimalFormat ff4=new DecimalFormat("#,###,###0.0000"); //小數(shù)點(diǎn)后保留四位小數(shù)(保留末尾的0)
(2)double ss=12345.97468;時(shí)使用:
DecimalFormat ff4=new DecimalFormat("#,###,###.0000"); //小數(shù)點(diǎn)后保留四位小數(shù)(保留末尾的0)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java連接 JDBC基礎(chǔ)知識(shí)(操作數(shù)據(jù)庫:增刪改查)
這篇文章主要介紹了Java連接 JDBC基礎(chǔ)知識(shí),包括操作數(shù)據(jù)庫之增刪改查操作,需要的朋友可以參考下2021-04-04
Springboot如何實(shí)現(xiàn)自定義異常數(shù)據(jù)
這篇文章主要介紹了Springboot如何實(shí)現(xiàn)自定義異常數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Spring中@Repository注解的作用和用法以及和@Mapper的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Spring中@Repository注解的作用和用法以及和@Mapper的區(qū)別的相關(guān)資料,注解的作用是標(biāo)識(shí)一個(gè)類為數(shù)據(jù)訪問對象,并由Spring框架進(jìn)行實(shí)例化和管理,需要的朋友可以參考下2023-09-09
Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場景
這篇文章主要為大家介紹了Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場景剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,2022-03-03
Java 內(nèi)存模型中的happen-before關(guān)系詳解
這篇文章主要為大家介紹了Java 內(nèi)存模型中的happen-before關(guān)系示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
mybatis打印的sql日志不寫入到log文件的問題及解決
這篇文章主要介紹了mybatis打印的sql日志不寫入到log文件的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Spring中@ConditionalOnProperty注解的作用詳解
這篇文章主要介紹了Spring中@ConditionalOnProperty注解的作用詳解,@ConditionalOnProperty注解主要是用來判斷配置文件中的內(nèi)容來決定配置類是否生效用的,如果條件不匹配,則配置類不生效,需要的朋友可以參考下2024-01-01
Springboot+Spring Security實(shí)現(xiàn)前后端分離登錄認(rèn)證及權(quán)限控制的示例代碼
本文主要介紹了Springboot+Spring Security實(shí)現(xiàn)前后端分離登錄認(rèn)證及權(quán)限控制的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11

