Java String format()的使用

不同类型的格式化举例

%s 字符串类型      String.format("%s","Hello")                           Hello

%c 字符类型          String.format("%c",'H')                                 H

%b 布尔类型          String.format("%b","Hello".equal("Hello"))   true

%d 整数类型          String.format("%d",9)                                   9

%+d                       String.format("%+d",9)                                 +9

%05d                     String.format("%05d",9)                              00009

% 5d                      String.format("% 5d",9)                                    9

%x 十六进制          String.format("%x",100)                               64

%o 八进制             String.format("%o",100)                               144

%f 浮点类型           String.format("%f",50*0.85)                         42.500000

%,f                         String.format("%,f",9999.99)                        9,999.990000

%(f                         String.format("%(f",-99.99)                          (99.990000)

%#x                       String.format("%#x",99)                               0x63

%#o                       String.format("%#o",99)                               0143

<格式化前一个      String.format("%f---%<3.2f",99.99)              99.990000---99.99

%a 十六进制浮点   String.format("%a",50*0.85)                        0x1.54p5

%e 指数                 String.format("%e",50*0.85)                        4.250000e+01

%g 通用浮点类型   String.format("%g",50*0.85)                       42.5000

%h 散列码              String.format("%h",'A')                               41

%% 百分比             String.format("%d%%", 85)                       85%

%n 换行符

%tc 日期时间         String.format("%tc",new Date())                 全部日期和时间信息

%tF                        String.format("%tF",new Date())                 年-月-日

%tD                        String.format("%tD",new Date())                月/日/年

%tr                         String.format("%tr",new Date())                 HH:MM:SS PM

%tT                        String.format("%tT",new Date())                HH:MM:SS

%tR                        String.format("%tR",new Date())               HH:MM

你可能感兴趣的:(Java,java)