【JAVA之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代表着取所要格式化數字的小數點位數,從小數點後面開始,不足位數補零

附錄(待補齊)

需求 寫法
百分號 %%
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章