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, and 0x 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 of 0.

  • precision is a decimal that specifies how many digits to display after the decimal point of a floating point value that is formatted with type f, F, or %, or before and after the decimal point of a floating point value that is formatted with type g, r, and p.

    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 by 100, and displays the number in fixed format, f, followed by percent sign.
    • b is for binary format, and outputs numbers in base 2.
    • c is for character; it converts integers to corresponding Unicode characters.
    • d is for decimal integer; it outputs numbers in base 10.

      Use Number.toString() method.

    • e is for exponent notation for floating point and decimal numbers; it prints the number in scientific notation, using e to indicate exponent.

      For example, it would print 345 as 3.45e2.

      Use Number.toExponential() method.

    • E is for exponent notation for floating point and decimal numbers; it prints the number in scientific notation, using E to indicate exponent.

      For example, it would print 345 as 3.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 as f; also converts nan to NAN, and inf to INF. 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 to p 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 of 1.

      Regardless of precision, positive infinity is rendered as inf, negative infinity as -inf, positive zero as 0, negative zero as -0, and NaN as nan.

      Use Number.toPrecision() method.

    • G is general format for floating point and decimal values, same as g. Also switches to E 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 to p 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 of 1.

      Regardless of precision, positive infinity is rendered as inf, negative infinity as -inf, positive zero as 0, negative zero as -0, and NaN as nan.

      Use Number.toPrecision() method.

    • n is general format for floating point and decimal number, same as g. 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 as 1.100,1 in France.

    • o is for octal format; it outputs numbers in base 8.
    • p is rounded percentage; like r type, but multiplied by 100 and with suffix %.
    • r is rounded to precision significant digits, padded with zeros whenever necessary, same as for f type. If precision is not specified, behaves like g 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 or 10-6) as 1.00μ, and it would render tera (1,000,000,000,000 or 1012) as 1.00T.

    • x is for hexadecimal format; it outputs numbers in base 16, using lower-case letters for digits over 9.

      a for 10, b for 11, c for 12, d for 13, e for 14, and f for 15.

    • X is for hexadecimal format; it outputs numbers in base 16, using upper-case letters for digits over 9.

      A for 10, B for 11, C for 12, D for 13, E for 14, and F for 15.

    • None is the same as s for strings, and d for integers. It is similar to g for floating point and decimal values, with at least one digit past the decimal point, and a default precision of 12.