星空网 > 软件开发 > Java

String.format Tutorial

String format(String format, Object... args)

The format specifiers for general, character, and numeric types have the following syntax:

%[argument_index$][flags][width][.precision]conversion

  • argument_index is a decimal integer indicating the position of the argument in the argument list. It starts from "1$".
  • flags is a set of  characters that modify the output format.
FlagGeneralCharacterIntegerFloating PointDate/TimeDescription
'-'YYYYYThe result will be left-justified
'#'Y-YY-The result should use a conversion-dependent alternate form
'+'--YY-The result will always include a sign
' ' --YY-The result will include a leading space for positive values
'0'--YY-The result will be zero-padded 
','--YY-The result will include locale-specific grouping separators 
'('--YY-The result will enclose negative numbers in parentheses 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • width is a non-negative decimal interger indicating the minimum number of the number of characters to be written to the output.
  • For the floating-point conversions 'e''E', and 'f' the precision is the number of digits after the decimal separator. If the conversion is 'g' or 'G', then the precision is the total number of digits in the resulting magnitude after rounding. If the conversion is 'a' or 'A', then the precision must not be specified.
  • conversion are divided into the following categories:

    General, Character, Numeric(Integer, Floating Point), Date/Time, Percent, Line Separator

ConversionArgument CategoryDescription
'b''B'generalIf the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true".
'h''H'generalIf the argument arg is null, then the result is "null". Otherwise, the result is obtained by invoking Integer.toHexString(arg.hashCode()).
's''S'generalIf the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invokingarg.toString().
'c''C'characterThe result is a Unicode character
'd'integralThe result is formatted as a decimal integer
'o'integralThe result is formatted as an octal integer
'x''X'integralThe result is formatted as a hexadecimal integer
'e''E'floating pointThe result is formatted as a decimal number in computerized scientific notation
'f'floating pointThe result is formatted as a decimal number
'g''G'floating pointThe result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding.
'a''A'floating pointThe result is formatted as a hexadecimal floating-point number with a significand and an exponent
't''T'date/timePrefix for date and time conversion characters. See Date/Time Conversions.
'%'percentThe result is a literal '%' ('\u0025')
'n'line separatorThe result is the platform-specific line separator



原标题:String.format Tutorial

关键词:string

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流