String
目前文章所包含的有以上内容
字符串格式化
首先是上官方API(源地址)
Modifier and Type | Method and Description |
---|---|
static String | format(Locale l, String format, Object… args) Returns a formatted string using the specified locale, format string, and arguments. |
static String | format(String format, Object… args) Returns a formatted string using the specified format string and arguments. |
format
引用官方API(跳转地址)
public static String format(String format,Object… args)
Returns a formatted string using the specified format string and arguments.
The locale always used is the one returned by Locale.getDefault().
Parameters:
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
format
引用官方API(跳转地址)
public static String format(Locale l,String format,Object… args)
Returns a formatted string using the specified locale, format string, and arguments.
Parameters:
l - The locale to apply during formatting. If l is null then no localization is applied.
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification
简单说,2个格式化方法在于缺省Locale时,Locale就是Locale.getDefault(),String就是要格式化的模板,Object就是这个模板对应的各个值,如果这些对应不上的话,就会抛出IllegalFormatException (非法格式异常)
format之%f,%g
System.out.println(String.format("%.2g",12.34)); //12
System.out.println(String.format("%.2g",123.34)); //1.2e+02
System.out.println(String.format("%.5g",12.34)); //12.340
System.out.println(String.format("%.2f",12.1234)); //12.12
System.out.println(String.format("%.5f",12.1234)); //12.12340
结论:
%.ng代表着取所要格式化数字的位数,从左边第一位开始,不足补零,注意,这里如果整数位数比n还小的话,得到的String将会是科学计数法
%.nf代表着取所要格式化数字的小数点
位数,从小数点后面开始,不足位数补零
附录(待补齐)
需求 | 写法 |
---|---|
百分号 | %% |