D Documentation  
Format string
Format strings consist of characters interspersed with format specifications. Characters are simply copied to the output (such as putc) after any necessary conversion to the corresponding UTF-8 sequence.

A format specification always starts with a percent '%' character. For writing a literal '%' you just immediately follow it by another '%'.

Example: "100%%" will result in "100%".

Each format specification consumes an argument following the format string. It describes how the argument should be printed. It consists of the follwing elements:

  1. The literal '%' character which signals the start of a format specification.

  2. Flags (optional):
    '-' will left-justify the resulting string in the field.
    '+' will prefix any positive number with a plus sign.
    '#' means "Use alternative formatting".
    • For octal format ('o') this will require that the string starts with a zero digit.
    • For hexadecimal format ('x','X') this will prefix the string with '0x' or '0X', if the value is non-zero.
    • For floating point format ('e','E','f','F') this will always insert the decimal point
    • For the automatic floating point format ('g','G') this will prohibit the elusion of trailing zeros.

  3. The minimum field width (optional) as numerical value. If it is '*', the next argument (which must be an int) is taken as the minimum field width, a negative value here will do the same as the '-' flag (left-justify).

  4. The precision (optional) which must start with a dot, followed by either a numerical value or '*'. '*' will do almost the same as in the minimum field with: the next argument (which must be an int) is taken as the precision, a negative value here will set the precision to zero.

  5. The formatting character (required). This character does the main conversion. Possible are:

    • 's': String
      The corresponding argument is formatted in a manner consistent with its type.

    • 'b','d','o','x','X': Binary, decimal, octal, hex (lower case), hex (upper case)
      The corresponding argument must be an integral type and is formatted as an integer.
      Only 'd' respects signed arguments.

    • 'e','E': Exponential floating point format
      Formats a floating point number with one digit before the decimal point plus precision digits after, followed by either 'e' or 'E' (depending on the formatting character used) and the exponent with at least 2 digits. The default precision is 6.

    • 'f','F': Floating point format (decimal)
      At least one digit before the decimal point plus precision digits after. Default precision is 6.

    • 'g','G': Automatic floating point format
      This will use the decimal floating point format only if the exponent is greater than -5 or less than precision. The precision specifies the number of significant digits, and defaults to one.

    • 'a','A': Hexadecimal floating point format
      Formats a floating point number starting with '0x' (or '0X' respectively) with one hexadecimal digit before the decimal point plus precision hexadecimal digits after, followed by either 'p' or 'P' (depending on the formatting character used) and the decimal (!) exponent to the base of 2. If no precision is given, as many hexadecimal digits as necessary to exactly represent the mantissa are generated.

Created using PHP docwiki written by Markus Dangl. Best viewed with Mozilla Firefox.