Skip to article

 

Format String Syntax

All characters except format specifiers are copied unchanged into the resulting string. The standard character to start a format specifier is the % character (percent sign), To display the % sign itself, its doubling %% is used.

Format specifier structure

The format specifier is:

%[flags][width][.accuracy]type 

The required components are the start character of the specifier format (%) and type.

Flags

Sign Name of the sign Value In the absence of this sign Note
- minus the output value is left aligned within minimum field width by right  
+ plus always indicate the sign (plus or minus) for the decimal output numerical value only for negative numbers  
  space place a space before the result if the first character of the value not a sign output can start with a number The + character has higher priority than the space character. ISPOLAIS for decimal numeric results only.
# octothorpe "alternative form" of outputting the value   when displaying numbers in the format, a special feature will be indicated before the number format (see below).
0 zero pad the field to the width specified in the width field escape sequence, symbol 0 pad with spaces if the minus '-' flag is specified, this flag is also ignoredя.

Width

Width(decimal) specifies the minimum field width (including sign for numbers). If the value representation is larger than the field width, then the entry goes outside the field (for example, %2i for the value 100 will give a field value of three characters) if the value representation is less than specified number, it will be padded (by default) with spaces on the left, behavior can be modified by preceding flags. If this method is specified negative width modifier, it is considered that the - flag is set, and the width modifier value is set to absolute.

Accuracy

The precision is specified as a point followed by a decimal number if number is missing (only a dot is present), then it is assumed that the number is zero. The dot is used to indicate precision even in case when a comma is output when outputting floating point numbers.

Type

The type indicates a specific representation of the output value. Recorded as one character. Unlike other fields, it is required.

Values ​​of type: