好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

聊聊DecimalFormat的用法及各符号的意义

DecimalFormat的用法及各符号的意义

符号 位置 本地化? 含义
0 数字 阿拉伯数字
# 数字字 阿拉伯数字,如果不存在则显示为空
. 数字 小数分隔符或货币小数分隔符
- 数字 减号
, 数字 分组分隔符
E 数字 分隔科学计数法中的尾数和指数。在前缀或后缀中无需加引号。
; 子模式边界 分隔正数和负数子模式
% 前缀或后缀 乘以 100 并显示为百分数
/u2030 前缀或后缀 乘以 1000 并显示为千分数
¤(/u00A4) 前缀或后缀 货币记号,由货币符号替换。如果两个同时出现,则用国际货币符号替换。如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符。
' 前缀或后缀 用于在前缀或或后缀中为特殊字符加引号,例如 "'#'#" 将 123 格式化为 "#123"。要创建单引号本身,请连续使用两个单引号:"# o''clock"。
       
       

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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时注意事项

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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( "#,###,###.######" );  绿色区域为保留的小数位数(四舍五入)----但不会保                                                                                                     留末尾的 0 ) --------------------整数的话就没有小数位

 

  DecimalFormat ff2= new DecimalFormat( "#,###,###.0000" ); //小数点后保留四位小数(保留末尾的0) //类似于                                                                                                              round()函数---四舍五入

  DecimalFormat ff3= new DecimalFormat( "#,###,###.00000" ); //小数点后保留五位小数(保留末尾的0)

 

  DecimalFormat ff4= new DecimalFormat( "#,###,###0.0000" ); //小数点后保留四位小数(保留末尾的0)

  DecimalFormat ff5= new DecimalFormat( "#,###,###0.00000" ); //小数点后保留五位小数(保留末尾的0)

 

  System.out.println( "#,###,###.######: " +ff.format(ss));

  System.out.println( "#,###,###.0000  : " +ff2.format(ss));  //类似于round()函数---四舍五入

  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));

     }

}

结果:

#,###,###.######: 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 (有问题) ---千位符位置有问题

总结一下吧

(1)double ss2=0.23;时使用:

?

1

DecimalFormat ff4= new DecimalFormat( "#,###,###0.0000" ); //小数点后保留四位小数(保留末尾的0)

(2)double ss=12345.97468;时使用:

?

1

DecimalFormat ff4= new DecimalFormat( "#,###,###.0000" ); //小数点后保留四位小数(保留末尾的0)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_28114159/article/details/105862909

查看更多关于聊聊DecimalFormat的用法及各符号的意义的详细内容...

  阅读:27次