?Java?,String.format()
?System.out.printf()
?????????????????????????,??????????????????,??????????????????????????????????:
%d
:?????%x
?%X
:??????(x????,X????)%o
:?????%f
:???(??????6?)%e
?%E
:???????????(e????,E????)%g
:???????????%f?%e%a
?%A
:?????????(a????,A????)%s
:???%c
:??%b
:???%n
:???%%
:???(%)
??,????????????????????????,??:
-
:???+
:?????0
:?0??#
:??????(??????0x??)- ??:????????
????????????,??:
]
:???5??????,?????????%.2f
:?????????????%-5s
:???5???????,???,?????????
??????????:
public class FormatExample { public static void main(String[] args) { int intValue = https://www.yisu.com/ask/42;"Hello, World!"; System.out.printf("Integer: %d%n", intValue); System.out.printf("Double: %.2f%n", doubleValue); System.out.printf("String: %-10s%n", stringValue); } }
??:
Integer: 42 Double: 3.14 String: Hello, World!