文章目录
1.代码中简单使用
String.format("%.2f", 2.0f);
2.源码调用的方法
* Returns a localized formatted string, using the supplied format and arguments,
* 返回本地化格式的字符串,使用提供的格式和参数
* using the user's default locale.
* 使用用户默认的本地语言环境
* <p>If you're formatting a string other than for human
* consumption, you should use the {@code format(Locale, String, Object...)}
* 格式化字符串如果使用其他语言环境,你应该使用format(Locale, String, Object...)
* overload and supply {@code Locale.US}. See
* "<a href="../util/Locale.html#default_locale">Be wary of the default locale</a>".
*
* @param format the format string (see {@link java.util.Formatter#format})
* @param args
* the list of arguments passed to the formatter. If there are
* more arguments than required by {@code format},
* additional arguments are ignored.
* @return the formatted string.
* @throws NullPointerException if {@code format == null}
* @throws java.util.IllegalFormatException
* if the format is invalid.
* @since 1.5
*/
public static String format(String format, Object... args) {
return format(Locale.getDefault(), format, args);
}
public static String format(Locale locale, String format, Object... args) {
if (format == null) {
throw new NullPointerException("format == null");
}
int bufferSize = format.length() + (args == null ? 0 : args.length * 10);
Formatter f = new Formatter(new StringBuilder(bufferSize), locale);
return f.format(format, args).toString();
}
3.相关类-Formatter
在Formatter类的释义中有如下信息:
Format strings consist of plain text interspersed with format specifiers, such as {@code “name: %s weight: %03dkg\n”}. Being a Java string, the usual Java string literal backslash escapes are of course available.
格式字符串由纯文本组成,其中穿插了格式说明符,如"name: %s weight: %03dkg\n"
中的%s
、%03d
。作为java字符串,反斜杠转移符也是支持的
Format specifiers (such as {@code “%s”} or {@code “%03d”} in the example) start with a {@code %} and describe how to format their corresponding argument. It includes an optional argument index, optional flags, an optional width, an optional precision, and a mandatory conversion type.
格式说明符以%
开头,描述如何格式化相应参数。格式说明符包括可选的参数索引、可选的标记、可选的宽度、可选的精度、强制类型转换
3.1可选的参数索引
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
%2$s 中的2 指定显示第2个参数 | format("%2 s , s,%1 s,s",“小明”,“小李”) | 小李,小明 | |
< | 复用同一个参数 | format("%o %<d %<x", 64); | 100 64 40 |
3.2可选的标记
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
, | 分割大数据 | format("%,d", 1024); | 1,234 |
+ | 总是显示正数、负数符号 | format("%+d, %+4d", -5, 5); | -5, +5 |
非负数有一个前导空格 | String.format(“x% d% 5d”, 4, -4); | x 4 -4 | |
( | 把括号括在负数上 | format("%(d, %(d, %(6d", 12, -12, -12); | 12, (12), (12) |
- | 对齐 需要宽度; -默认右对齐, - 号左对齐; | format("%-6dx", 5); format("%-3C,%3C", ‘d’, 0x65); | 5 x D , E |
0 | 用前导零填充数字 | format("%07d, %03d", 4, 5555); | 0000004, 5555 |
# | 变形 仅八进制、十六进制; | format("%o %#o %d", 010, 010,010); format("%x %#x %d", 0x12, 0x12,0x12); | 10 010 8 12 0x12 18 |
3.3可选的宽度
The width is a decimal integer specifying the minimum number of characters to be used to represent the argument.
宽度是一个十进制整数,指定参数显示的最小字符数。
注:不能使用宽度来截断字段
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
5前面有两个空格 | format("%3d",5); | 5 |
3.4可选的精度
The precision is a {@code .} followed by a decimal integer
精度是.
后面跟的整形数字
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
. | 小数点后面跟几位 | format("%.1f", 123.456f); | 123.5 |
3.5强制类型转换
3.1非日期/时间转换类型
3.1.1字符串转换
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
s | 字符串 | format("%s %s", “hello”, “Hello”); | hello Hello |
S | 大写字符串 | format("%S %S", “hello”, “Hello”); | HELLO HELLO |
3.1.2字符转换
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
c | 字符 | format("%c %c", ‘d’, ‘E’); | d E |
C | 大写字符 | format("%C %C", ‘d’, ‘E’); | D E |
3.1.3整数转换
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
d | 十进制 | format("%d", 26); | 26 |
o | 八进制 | format("%o", 032); | 32 |
x | 十六进制 | format("%x %X", 0x1a, 0x1a); | 1a 1A |
3.1.4浮点数转换
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
f | 十进制小数 | format("%f", 123.456f); format("%.1f", 123.456f); format("%1.5f", 123.456f); format("%10f", 123.456f); format("%6.0f", 123.456f); | 123.456001 123.5 123.45600 123.456001 123 |
e,E | 指数浮点数 | format("%e", 123.456f); format("%.1e", 123.456f); format("%1.5E", 123.456f); format("%10E", 123.456f); format("%6.0E", 123.456f); | 1.234560e+02 1.2e+02 1.23456E+02 1.234560E+02 1E+02 |
g,G | 十进制或指数取决于数的大小 | format("%g %g", 0.123, 0.0000123); | 0.123000 1.23000e-05 |
a,A | 十六进制小数 | format("%a", 123.456f); | 0x1.edd2f2p6 |
3.1.5布尔值转换
支持Boolean值。null被认为是false,其他类型实例为true
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
b,B | 布尔值 | format("%b %b", true, false); format("%B %B", true, false); format("%b", null); format("%b", “hello”); | true false TRUE FALSE false true |
3.1.6hash值转换
调用参数的hashCode方法,支持所有类型
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
h,H | 十六进制hashcode | format("%h", this); format("%H", this); format("%h", null); | 190d11 190D11 null |
3.1.7无参转换
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
% | 输出%字符 | format("%d%%", 50); | 50% |
n | 换行 | format(“first%nsecond”); | first\nsecond |
3.2日期/时间转换
日历、日期、毫秒值均可以作为日期/时间参数。如果类型有错误,则返回1970-01-01 00:00:00 UTC
标记 | 作用 | 例子 | 结果 |
---|---|---|---|
ta | 周内第几天(缩写) | format("%ta", cal, cal); | Tue |
tA | 周内第几天(全) | format("%tA", cal, cal); | Tuesday |
tb | 月份第几月(缩写)) | format("%tb", cal); | Apr |
tB | 月份第几月(全) | format("%tB", cal); | April |
tc | 时间,请勿使用 | format("%tc", cal); | Tue Apr 01 16:19:17 CEST 2008 |
tC | 世纪,两位数 | format("%tC", cal); | 20 |
td | 月份第几天,两位数 | format("%td", cal); | 01 |
tD | 美国日期格式,请勿使用 | format("%tD", cal); | 04/01/08 |
te | 月中的1天(1-31) | format("%te", cal); | 1 |
tF | ISO 8601标准下的全面日期格式(YYYY-MM-DD) | format("%tF", cal); | 2008-04-01 |
th | %tb的同义词 | ||
tH | 2位数表示小时,24小时制(00-23) | format("%tH", cal); | 16 |
tI | 2位数表示小时,12小时制(01-12) | format("%tI", cal); | 04 |
tj | 三位数表示一年中的第几天(001-366) | format("%tj", cal); | 092 |
tk | 表示小时,24小时制(0-23) | format("%tk", cal); | 16 |
tl | 表示小时,12小时制(1-12) | format("%tl", cal); | 4 |
tL | 毫秒数 | format("%tL", cal); | 359 |
tm | 2位数一年中的第几个月(01-12) | format("%tm", cal); | 04 |
tM | 2位数分钟 | format("%tM", cal); | 08 |
tN | 纳秒 | format("%tN", cal); | 359000000 |
tp | 上午或下午 | format("%tp %Tp", cal, cal); | pm PM |
tQ | 自时代以来的毫秒值 | format("%tQ", cal); | 1207059412656 |
tr | 完整的12小时时间 %tI:%tM:%tS %Tp | format("%tr", cal); | 04:15:32 PM |
tR | 短时24小时制 | format("%tR", cal); | 16:15 |
ts | 自时代以来的秒值 | format("%ts", cal); | 1207059412 |
tS | 两位数秒(00-60) | format("%tS", cal); | 17 |
tT | 完整24小时制 %tH:%tM:%tS | format("%tT", cal); | 16:15:32 |
ty | 两位数年(00-99) | format("%ty", cal); | 08 |
tY | 四位数年 | format("%tY", cal); | 2008 |
tz | 时间区格林尼治时间偏移 | format("%tz", cal); | +0100 |
tZ | 本地时区缩写 | format("%tZ", cal); | CEST |