# 格式化
# java.util——字符串格式化操作Formatter
| 说明符 | 适用于 | 输出 |
|---|---|---|
| %a | 浮点数 (除了BigDecimal) | 浮点数的十六进制输出 |
| %b | 任何类型 | 如果为非空则为“true”,为空则为“false” |
| %c | 字符 | Unicode字符 |
| %d | 证书(包括byte, short, int, long, bigint) | 十进制整数 |
| %e | 浮点数 | 科学计数的十进制数 |
| %f | 浮点数 | 十进制数 |
| %g | 浮点数 | 十进制数,根据值和精度可能以科学计数法显示 |
| %h | 任何类型 | 通过hashCode()方法输出的16进制数 |
| %n | 无 | 平台相关的换行符 |
| %o | 整数(包括byte, short, int, long, bigint) | 八进制数 |
| %s | 任何类型 | 字符串 |
| %t | 日期/时间 (包含long, Calendar, Date 和TemporalAccessor) | %t是日期/时间转换的前缀。后面还需要跟其他的标识,请参考下面的日期/时间转换。 |
| %x | 整数(包含byte, short, int, long, bigint) | 十六进制字符串 |
# 具体日期和时间格式
注意:使用
“%T”替换下面的“%t”可以将输出结果变成大写形式。
| 标识 | 注释 |
|---|---|
| %tA | 星期几的全名,例如 “Sunday“, “Monday“。 |
| %ta | 星期几的缩写,例如 “Sun“, “Mon“。 |
| %tB | 月份的全名,例如 “January“, “February“。 |
| %tb | 月份的缩写,例如 “Jan“, “Feb“。 |
| %tC | 年的世纪部分的格式为两位数,从 “00“到“99”。 |
| %tc | 日期和时间的格式为 “%ta %tb %td %tT %tZ %tY” 如 “Fri Feb 17 07:45:42 PST 2017“。 |
| %tD | 格式为 “%tm/%td/%ty“ 的日期。 |
| %td | 两位的日期格式,从 “01”到 “31“。 |
| %te | 没有前导0的日期,从 “1” 到 “31”。 |
| %tF | 使用 “%tY-%tm-%td“ 格式的 ISO 8601 日期。 |
| %tH | 24小时制的小时,从 “00” 到 “23“。 |
| %th | 同 %tb。 |
| %tI | 12小时制的小时,从 “01” 到 “12“。 |
| %tj | 带前导0的年中的日期,从 “001” 到“366“。 |
| %tk | 没有前导0的24小时制,从 “0” 到 “23“。 |
| %tl | 没有前导0的12小时制,从 “1” 到“12“。 |
| %tM | 带前导0的分钟,从 “00” 到“59“。 |
| %tm | 带前导0的月份,从 “01” 到 “12“。 |
| %tN | 带前导0的9位纳秒数,从 “000000000” to “999999999”. |
| %tp | 和区域相关的 “am” or “pm” 标记。 |
| %tQ | 1970年1月1日00:00:00 UTC 以来的毫秒。 |
| %tR | 24小时制的时间,如:“%tH:%tM“。 |
| %tr | 2位数字格式的秒,从 “00” 到 “60”。 “60” 需要支持闰秒。 |
| %ts | 1970年1月1日00:00:00 UTC以后的秒数。 |
| %tT | 24小时制的时分秒,如: “%tH:%tM:%tS“。 |
| %tY | 4位的年份格式,从 “0000” 到 “9999“。 |
| %ty | 2位的年份格式,从 “00” 到 “99“。 |
| %tZ | 时区缩写,如:“UTC“, “PST“。 |
| %tz | 与GMT的时区偏移量,如 -0800。 |
System.out.println(String.format(Locale.ENGLISH, "%tA", LocalDateTime.now()));//Wednesday
System.out.println(String.format("%tA", LocalDateTime.now()));//星期三
System.out.println(String.format("%tF", LocalDateTime.now()));//2021-01-20
// 下列格式,不适用java8时间
System.out.println(String.format("%tQ", new Date()));//1611121921492
1
2
3
4
5
6
2
3
4
5
6
# 参数索引
参数索引是以 $结尾,在%后面的数字,用于指定在参数列表中的参数。 参数索引从 1 开始。
String.format("%2$s", 32, "Hello"); // 输出:"Hello"
1
# 格式化整数
使用%d格式说明符,您可以使用所有整数类型的参数,包括byte,short,int,long 和BigInteger。默认格式:
String.format("%d", 93); // 输出:93
1
- 指定宽度:
String.format("%20d", 93); // 输出:| 93|
1
- 指定宽度内的左对齐:
String.format("%-20d", 93); // 输出:|93 |
1
- 用零填充:
String.format("%020d", 93); // 输出:|00000000000000000093|
1
- 用”+”号打印正数(负数总是包含”-“):
String.format("%+20d", 93); // 输出:| +93|
String.format("%+20d", -93); // 输出:| -93|
1
2
2
- 正数之前的空格,按正常值计算负数的“-”。
String.format("% d", 93); // 输出:| 93|
String.format("% d", -36); // 输出:|-36|
1
2
2
- 使用和区域相关的千位分隔符,美国的是“,”:
String.format("%,d", 10000000); // 输出:|10,000,000|
1
- 中国的也一样:
String.format(Locale.CHINA, "%,d", 10000000)// 输出:|10,000,000|
1
- 使用左边括号括起来可以跳过“-”。
String.format("%(d", -36); // 输出:|(36)|
1
- 八进制输出。
String.format("%o"), 93); // 输出:135
1
- 十六进制输出。
String.format("%x", 93); // 输出:5d
1
- 八进制和十六进制输出的替代表示。
带前导0的八进制和带前导”0x“的十六进制输出:
String.format("%#o", 93); // 输出:0135
String.format("%#x", 93); // 输出:0x5d
String.format("%#X", 93); // 输出:0X5D
1
2
3
2
3
# 字符串和字符转换
默认格式。
- 打印整个字符串。
String.format("%s", "Hello World"); // 输出:"Hello World"
1
- 指定字段长度。
String.format("%20s", "Hello World"); // 输出:| Hello World|
1
- 左对齐文本。
String.format("%-20s", "Hello World"); // 输出:|Hello World |
1
- 指定最大字符数。
String.format("%.5s", "Hello World"); // 输出:|Hello|
1
- 指定宽度和最大字符数。
String.format("%20.5s", "Hello World"); // 输出:| Hello|
1
# DecimalFormat数字格式化的主要功能及使用方法
| 符号 | 位置 | 本地化? | 含义 |
|---|---|---|---|
0 | 数字 | 是 | 阿拉伯数字 |
# | 数字 | 是 | 阿拉伯数字如果不存在就显示为空 |
. | 数字 | 是 | 小数分隔符或货币小数分隔符 |
- | 数字 | 是 | 减号 |
, | 数字 | 是 | 分组分隔符 |
E | 数字 | 是 | 分割科学技术法中的尾数和指数。在前缀和后缀中无需添加引号 |
; | 子模式边界 | 是 | 分隔正数和负数子模式 |
% | 前缀或后缀 | 是 | 乘以100并显示为百分数 |
/u2030 | 前缀或后缀 | 是 | 乘以1000并显示为千分数 |
¤ (\u00A4) | 前缀或后缀 | 否 | 货币记号,由货币符号替换。如果两个同时出现,则用国际货币符号替换。如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符 |
' | 前缀或后缀 | 否 | 用于在前缀或或后缀中为特殊字符加引号,例如 "'#'#" 将 123 格式化为 "#123"。要创建单引号本身,请连续使用两个单引号:"# o''clock" |
# 最基本的使用-0和#配合使用
// 占位符比实际数字多
new DecimalFormat("00.00").format(3.14567);//结果:03.15
new DecimalFormat("0.000").format(3.14);//结果:3.140
new DecimalFormat("00.000").format(3.14);//结果:03.140
new DecimalFormat("##.##").format(3.14567);//结果:3.15
new DecimalFormat("#.###").format(3.14);//结果:3.14
new DecimalFormat("##.###").format(3.14);//结果:3.14
// 占位符比实际数字少
new DecimalFormat("00.00").format(13.14567);//结果:13.15
new DecimalFormat("0.000").format(13.14567);//结果:13.146
new DecimalFormat("0.00").format(13.14567);//结果:13.15
new DecimalFormat("#.###").format(13.145678);//结果:13.146
new DecimalFormat("##.##").format(13.14567);//结果:13.15
new DecimalFormat("#.##").format(13.14567);//结果:13.15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
0占位符- 比实际数字的位数多,不足的地方用0补上。
- 比实际数字的位数少:整数部分不改动,小数部分,四舍五入(其实并不是四舍五入,而是默认的
RoundingMode.HALF_EVEN方式,下面会讲到DecimalFormat的舍入方式)。
#占位符- 比实际数字的位数多,不变。
- 比实际数字的位数少:整数部分不改动,小数部分,四舍五入(其实并不是四舍五入,而是默认的RoundingMode.HALF_EVEN方式,下面会讲到DecimalFormat的舍入方式)。
- 总结
- 格式化数字,保留两位小数,不足的小数部分用0代替,这时候,我们就可以使用:"0.00";
- 格式化数字,只保留有效数字,最多保留两位小数,这时候,我们就可以使用:"#.##"。
- 0和#配合使用,只能是"##00",不能是"00##",就是
#在前、0在后
# ,分组分隔符
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
1
2
3
4
5
2
3
4
5
# -减号
表示输出为负数, 要放在最前面
pi = 3.14;
System.out.println(new DecimalFormat("-0.00").format(pi));//-3.14
1
2
2
# % 将数字乘以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
1
2
3
4
2
3
4
%处理最前面不能放置之外,其他的地方都可以放置
# \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
1
2
3
4
2
3
4