Standard format specifier
Format of the specifier follows:
[[fill]align][sign][symbol][0][width][,][.precision][type]
where:
-
fill
is a character other than{
or}
.The fill works with one of the alignment options. If the alignment character is invalid, both fill and alignment are ignored.
-
align
is one of the following:<
forces the field to be left-aligned within the available space; this is the default setting.>
forces the field to be right-aligned within available space.=
forces padding after the sign, if any, but before the digits; used for printing numeric fields in the form+000000120
.^
forces the field to be centered within the available space.
-
sign
is one of the following:+
means that a sign must be used for both positive and negative numbers.-
means that a sign must be used for negative numbers only; this is the default setting." "
(space) means that a leading space must be used on positive numbers, and a minus sign on negative numbers.
-
symbol
is one of the following:$
, to represent currency#
, which is valid only for integers of specific output:0b
for binary,0o
for octal, and0x
for hexadecimal options.
0
enables zero-padding.,
(comma) signals to use a comma for a thousands separator.-
width
is a decimal integer that defines the minimum field width; if not specified, the content determines the width.If preceded by
0
, the field will be zero-padded. This produces the same result as alignment of=
and fill of0
. -
precision
is a decimal that specifies how many digits to display after the decimal point of a floating point value that is formatted with typef
,F
, or%
, or before and after the decimal point of a floating point value that is formatted with typeg
,r
, andp
.For non-number types, it indicates the maximum fields size, or how many characters to use in field content.
The precision field does not support integer values.
-
type
is one of the following:%
is for percentage. Multiplies the number by100
, and displays the number in fixed format,f
, followed by percent sign.b
is for binary format, and outputs numbers in base2
.c
is for character; it converts integers to corresponding Unicode characters.-
d
is for decimal integer; it outputs numbers in base10
.Use
Number.toString()
method. -
e
is for exponent notation for floating point and decimal numbers; it prints the number in scientific notation, usinge
to indicate exponent.For example, it would print
345
as3.45e2
.Use
Number.toExponential()
method. -
E
is for exponent notation for floating point and decimal numbers; it prints the number in scientific notation, usingE
to indicate exponent.For example, it would print
345
as3.45E2
.Use
Number.toExponential()
method. -
f
is for fixed floating point. Displays the number as a fixed-point number.Use
Number.toFixed()
method. -
F
is for fixed floating point, same asf
; also convertsnan
toNAN
, andinf
toINF
. Displays the number as a fixed-point number.Use
Number.toFixed()
method. -
g
is general format for floating point and decimal values.For a precision
p>=1
, it rounds the number top
significant digits, and formats the result depending on magnitude, either in fixed-point format, or in scientific notation.Both insignificant trailing zeros and decimal point are dropped if unnecessary.
A precision of
0
is treated the same as precision of1
.Regardless of precision, positive infinity is rendered as
inf
, negative infinity as-inf
, positive zero as0
, negative zero as-0
, and NaN asnan
.Use
Number.toPrecision()
method. -
G
is general format for floating point and decimal values, same asg
. Also switches toE
notation if the number is too large. Represents infinity and Nan as uppercase:INF
,-INF
,NAN
.For a precision
p>=1
, it rounds the number top
significant digits, and formats the result depending on magnitude, either in fixed-point format, or in scientific notation.Both insignificant trailing zeros and decimal point are dropped if unnecessary.
A precision of
0
is treated the same as precision of1
.Regardless of precision, positive infinity is rendered as
inf
, negative infinity as-inf
, positive zero as0
, negative zero as-0
, and NaN asnan
.Use
Number.toPrecision()
method. -
n
is general format for floating point and decimal number, same asg
. However, it uses current locale settings to insert the appropriate number separator characters.For example, one thousand one hundred and one-tenth would be rendered as
1,100.1
in United States and as1.100,1
in France. o
is for octal format; it outputs numbers in base8
.p
is rounded percentage; liker
type, but multiplied by100
and with suffix%
.r
is rounded toprecision
significant digits, padded with zeros whenever necessary, same as forf
type. Ifprecision
is not specified, behaves likeg
type.-
s
is for string format; it is the default type for string, and does not have to be specified.If used for floating point or decimal numbers, it is the metric prefix, SI for the International System of Units. It would render micro (
0.000001
or10-6
) as1.00μ
, and it would render tera (1,000,000,000,000
or1012
) as1.00T
. -
x
is for hexadecimal format; it outputs numbers in base16
, using lower-case letters for digits over9
.a
for10
,b
for11
,c
for12
,d
for13
,e
for14
, andf
for15
. -
X
is for hexadecimal format; it outputs numbers in base16
, using upper-case letters for digits over9
.A
for10
,B
for11
,C
for12
,D
for13
,E
for14
, andF
for15
. -
None is the same as
s
for strings, andd
for integers. It is similar tog
for floating point and decimal values, with at least one digit past the decimal point, and a default precision of12
.