DecimalFormat 介绍与使用
Java 中 浮点数的格式化
# 1_符号
符号 | 位置 | 本地化? | 含义 |
---|---|---|---|
0 | 数字 | 是 | 阿拉伯数字 |
# | 数字字 | 是 | 阿拉伯数字,如果不存在则显示为 0 |
. | 数字 | 是 | 小数分隔符或货币小数分隔符 |
- | 数字 | 是 | 减号 |
, | 数字 | 是 | 分组分隔符 |
E | 数字 | 是 | 分隔科学计数法中的尾数和指数。在前缀或后缀中无需加引号。 |
; | 子模式边界 | 是 | 分隔正数和负数子模式 |
% | 前缀或后缀 | 是 | 乘以 100 并显示为百分数 |
\u2030 | 前缀或后缀 | 是 | 乘以 1000 并显示为千分数 |
¤ (\u00A4 ) | 前缀或后缀 | 否 | 货币记号,由货币符号替换。如果两个同时出现,则用国际货币符号替换。如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符。 |
' | 前缀或后缀 | 否 | 用于在前缀或或后缀中为特殊字符加引号,例如 "'#'#" 将 123 格式化为 "#123" 。要创建单引号本身,请连续使用两个单引号:"# o''clock" 。 |
# 2_舍入
DecimalFormat
提供 RoundingMode
中定义的舍入模式进行格式化。默认情况下,它使用 RoundingMode.HALF_EVEN
。
# 3_基本使用
# 3.1_0
和#
配合使用
double pi = 3.1415927;//圆周率
//取一位整数
System.out.println(new DecimalFormat("0").format(pi));//3
//取一位整数和两位小数
System.out.println(new DecimalFormat("0.00").format(pi));//3.14
//取两位整数和三位小数,整数不足部分以0填补。
System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
//取所有整数部分
System.out.println(new DecimalFormat("#").format(pi));//3
//以百分比方式计数,并取两位小数
System.out.println(new DecimalFormat("#.##%").format(pi));//314.16%
/**
* 上面的代码就是网上很经典的案例,下面我们来分析另外的一个值
*/
pi=12.34567;
//取一位整数
System.out.println(new DecimalFormat("0").format(pi));//12
//取一位整数和两位小数
System.out.println(new DecimalFormat("0.00").format(pi));//12.35
//取两位整数和三位小数,整数不足部分以0填补。
System.out.println(new DecimalFormat("00.000").format(pi));// 12.346
//取所有整数部分
System.out.println(new DecimalFormat("#").format(pi));//12
//以百分比方式计数,并取两位小数
System.out.println(new DecimalFormat("#.##%").format(pi));//1234.57%
/**
* 扩展,如果是其他的数字会是下面的效果
*/
pi=12.34;
//整数
System.out.println(new DecimalFormat("6").format(pi));//612
System.out.println(new DecimalFormat("60").format(pi));//612
System.out.println(new DecimalFormat("06").format(pi));//126
System.out.println(new DecimalFormat("00600").format(pi));//00126
System.out.println(new DecimalFormat("#####60000").format(pi));//00126
//小数
System.out.println(new DecimalFormat(".6").format(pi));//12.6
System.out.println(new DecimalFormat(".06").format(pi));//12.36
System.out.println(new DecimalFormat(".60").format(pi));//12.36
System.out.println(new DecimalFormat(".0600").format(pi));//12.3406
System.out.println(new DecimalFormat(".6000").format(pi));//12.3406
System.out.println(new DecimalFormat(".600000##").format(pi));//12.340006
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
很明显
.
就是我们常用的小数点分隔符,前面是整数,后面是小数。1.整数:若是n个0,就从个位开始向高位填充,如果有值就是原来的值,没有就填充0。 若都是#,没有实际意义,不管是几个#,最后的结果都是原来的整数。 0和#配合使用,
只能是"##00",不能是"00##",就是#在前0在后
。实现是上面的合集。 2.小数:是可以保留小数点后几位的(几个0后或几个#)。 若n个0,就是保留n位小数,小数不足的部分用0填充。 若n个#,就是保留n位小数,小数不足部分没有就是没有。 0和#配合使用,只能是".00##",不能是".##00",就是0在前#在后
。实现和上面一样。 3.数字(1-9):上面的分析不是#就是0,如果是其他的数值会怎样呢? 上面的扩展很详细的说明这个问题。 整数:若没有0或#,默认在后面拼接整数;若有0或#,找到第一个0或#的位置,然后找出所有的0或#拼在一起,按照上面的规则,在第一个0或#出现的位置插入响应的格式化以后的值。 小数:若没有0或#,格式化是什么就显示什么;若有0或#,找出所有的0或#拼在一起,按照上面的规则,在小数点的后面插入响应的格式化以后的值。
# 4_科学计数法
在使用double的时候如果后面的小数为过多就会自动转换为科学计数法
pi = 123456789.3456;
System.out.println(new DecimalFormat("0E0").format(pi));//1E8
System.out.println(new DecimalFormat("0E00").format(pi));//1E08
System.out.println(new DecimalFormat("#E0").format(pi));//.1E9
System.out.println(new DecimalFormat("##E0").format(pi));//1.2E8
System.out.println(new DecimalFormat("###E0").format(pi));//123E6
System.out.println(new DecimalFormat("####E0").format(pi));//1.235E8
System.out.println(new DecimalFormat("#####E0").format(pi));//1234.6E5
System.out.println(new DecimalFormat("######E0").format(pi));//123.457E6
System.out.println(new DecimalFormat("#######E0").format(pi));//12.34568E7
System.out.println(new DecimalFormat("########E0").format(pi));//1.2345679E8
System.out.println(new DecimalFormat("#########E0").format(pi));//123456789E0
System.out.println(new DecimalFormat("##########E0").format(pi));//123456789.3E0
pi = 12345678.3456;
System.out.println(new DecimalFormat("0E0").format(pi));//1E7
System.out.println(new DecimalFormat("0E00").format(pi));//1E07
System.out.println(new DecimalFormat("#E0").format(pi));//.1E8
System.out.println(new DecimalFormat("##E0").format(pi));//12E6
System.out.println(new DecimalFormat("###E0").format(pi));//12.3E6
System.out.println(new DecimalFormat("####E0").format(pi));//1235E4
System.out.println(new DecimalFormat("#####E0").format(pi));//123.46E5
System.out.println(new DecimalFormat("######E0").format(pi));//12.3457E6
System.out.println(new DecimalFormat("#######E0").format(pi));//12.34568E7
System.out.println(new DecimalFormat("########E0").format(pi));//12345678E0
System.out.println(new DecimalFormat("#########E0").format(pi));//12345678.3E0
System.out.println(new DecimalFormat("##########E0").format(pi));//12345678.35E0
/**
* 0的个数决定最后输出结果的位数
* 并且与0的位置无关
*/
pi = 12345;
System.out.println(new DecimalFormat("###.##E0").format(pi));//12.345E3
System.out.println(new DecimalFormat("##0.##E0").format(pi));//12.345E3
System.out.println(new DecimalFormat("##0.0##E0").format(pi));//12.345E3
System.out.println(new DecimalFormat("##0.00000##E0").format(pi));//12.3450E3
System.out.println(new DecimalFormat("#00.0000##E0").format(pi));//12.3450E3
System.out.println(new DecimalFormat("#00.00000##E0").format(pi));//12.34500E3
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
30
31
32
33
34
35
36
37
38
39
总结: 1.使用科学计数法,首先保证
E
前面有0或者#,否则就不是科学计数法。 2.E
后面必须是0,0的个数对后面的显示是有影响的,多余就会填充0. 3.E
前面只有一个#,得到的结果肯定是.
开头的结果。 4.E
前面#与0的总个数决定后面的指数,具体:总个数和指数比较,如果指数的值大于总个数,那么得到的指数的值是个数的倍数;如果指数的值小于等于总个数,那么得到的指数的值等于总个数; 5.整个模式中的0的总个数决定最后输出结果的位数,并且与0的位置无关。 6.如果整数部分需要保留几位数,就使用几个0。
# 5_分组分隔符和减号
# 5.1_分组分隔符
pi = 1299792458;
//每三位以逗号进行分隔。
System.out.println(new DecimalFormat(",###").format(pi));//1,299,792,458
System.out.println(new DecimalFormat(",##").format(pi));//12,99,79,24,58
System.out.println(new DecimalFormat("###,##").format(pi));//12,99,79,24,58
2
3
4
5
上面的代码,最常用的就是千位分隔符。 不管模式中有多少个分隔符,最右边的那一个有效;每一组的个数就是最右边的分隔符之右的整数位数
# 5.2_减号
-
表示输出为负数, 要放在最前面
pi = 3.14;
System.out.println(new DecimalFormat("-0.00").format(pi));//-3.14
2
# 6_前缀、后缀
# 6.1_%
将数字乘以100
pi = 0.1234;
System.out.println(new DecimalFormat("0.00%").format(pi));//12.34%
System.out.println(new DecimalFormat("0%.00").format(pi));//12.34%
System.out.println(new DecimalFormat("%0.00").format(pi));//%12.34
2
3
4
%除了最前面不能放置之外,其他的地方都可以放置。
# 6.2_\u2030
将数字乘以1000
pi = 0.1234;
System.out.println(new DecimalFormat("0.00\u2030").format(pi));//123.40‰
System.out.println(new DecimalFormat("0.0\u20300").format(pi));//123.40‰
System.out.println(new DecimalFormat("\u20300.00").format(pi));//‰123.40
2
3
4
\u2030
和%
用法是一样的
# 6.3_¤(\u00A4)
本地化货币符号
如果连续出现两次,代表货币符号的国际代号。
pi = 1234.5678;
System.out.println(new DecimalFormat(",000.00¤").format(pi));//1,234.57¥
System.out.println(new DecimalFormat(",000.¤00").format(pi));//1,234.57¥
System.out.println(new DecimalFormat("¤,000.00").format(pi));//¥1,234.57
System.out.println(new DecimalFormat(",00¤0.¤00").format(pi));//1,234.57¥¥
System.out.println(new DecimalFormat("¤,000.¤00").format(pi));//¥1,234.57¥
System.out.println(new DecimalFormat(",000.00¤¤").format(pi));//1,234.57CNY
2
3
4
5
6
7
# 6.4_'
用于引用特殊的字符,作为前缀或后缀
pi = 4.5678;
System.out.println(new DecimalFormat("'#'0.00").format(pi));//#4.57
System.out.println(new DecimalFormat("'^ _ ^'0.00").format(pi));//^ _ ^4.57
//使用'本身作为前缀或后缀
System.out.println(new DecimalFormat("''0.00").format(pi));//'4.57
2
3
4
5