std.format - D Programming Language
Description: D Programming Language
Keywords: D programming language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# std.format

This package provides string formatting functionality using printf style format strings.

Submodule | Function Name | Description
--- | --- | ---
package | format | Converts its arguments according to a format string into a string.
package | sformat | Converts its arguments according to a format string into a buffer.
package | FormatException | Signals a problem while formatting.
write | formattedWrite | Converts its arguments according to a format string and writes the result to an output range.
write | formatValue | Formats a value of any type according to a format specifier and writes the result to an output range.
read | formattedRead | Reads an input range according to a format string and stores the read values into its arguments.
read | unformatValue | Reads a value from the given input range and converts it according to a format specifier.
spec | FormatSpec | A general handler for format strings.
spec | singleSpec | Helper function that returns a FormatSpec for a single format specifier.

Limitation This package does not support localization, but adheres to the rounding mode of the floating point unit, if available.

### Format Strings

The functions contained in this package use format strings. A format string describes the layout of another string for reading or writing purposes. A format string is composed of normal text interspersed with format specifiers. A format specifier starts with a percentage sign '%', optionally followed by one or more parameters and ends with a format indicator. A format indicator may be a simple format character or a compound indicator.
Format strings are composed according to the following grammar:

    FormatString:
        FormatStringItem FormatString
    FormatStringItem:
        Character
        FormatSpecifier
    FormatSpecifier:
        '%' Parameters FormatIndicator

    FormatIndicator:
        FormatCharacter
        CompoundIndicator
    FormatCharacter:
        see remark below
    CompoundIndicator:
        '(' FormatString '%)'
        '(' FormatString '%|' Delimiter '%)'
    Delimiter
        empty
        Character Delimiter

    Parameters:
        Position Flags Width Precision Separator
    Position:
        empty
        Integer '$'
        Integer ':' Integer '$'
        Integer ':' '$'
    Flags:
        empty
        Flag Flags
    Flag:
        '-'|'+'|' '|'0'|'#'|'='
    Width:
        OptionalPositionalInteger
    Precision:
        empty
        '.' OptionalPositionalInteger
    Separator:
        empty
        ',' OptionalInteger
        ',' OptionalInteger '?'
    OptionalInteger:
        empty
        Integer
        '*'
    OptionalPositionalInteger:
        OptionalInteger
        '*' Integer '$'

    Character
        '%%'
        AnyCharacterExceptPercent
    Integer:
        NonZeroDigit Digits
    Digits:
        empty
        Digit Digits
    NonZeroDigit:
        '1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
    Digit:
        '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'

Note FormatCharacter is unspecified. It can be any character that has no other purpose in this grammar, but it is recommended to assign (lower- and uppercase) letters.

Note The Parameters of a CompoundIndicator are currently limited to a '-' flag.

#### Format Indicator

The format indicator can either be a single character or an expression surrounded by %( and %). It specifies the basic manner in which a value will be formatted and is the minimum requirement to format a value.
The following characters can be used as format characters:

FormatCharacter | Semantics
--- | ---
's' | To be formatted in a human readable format. Can be used with all types.
'c' | To be formatted as a character.
'd' | To be formatted as a signed decimal integer.
'u' | To be formatted as a decimal image of the underlying bit representation.
'b' | To be formatted as a binary image of the underlying bit representation.
'o' | To be formatted as an octal image of the underlying bit representation.
'x' / 'X' | To be formatted as a hexadecimal image of the underlying bit representation.
'e' / 'E' | To be formatted as a real number in decimal scientific notation.
'f' / 'F' | To be formatted as a real number in decimal natural notation.
'g' / 'G' | To be formatted as a real number in decimal short notation. Depending on the number, a scientific notation or a natural notation is used.
'a' / 'A' | To be formatted as a real number in hexadezimal scientific notation.
'r' | To be formatted as raw bytes. The output may not be printable and depends on endianess.

The compound indicator can be used to describe compound types like arrays or structs in more detail. A compound type is enclosed within '%(' and '%)'. The enclosed sub-format string is applied to individual elements. The trailing portion of the sub-format string following the specifier for the element is interpreted as the delimiter, and is therefore omitted following the last element. The '%|' specifier may be used to explicitly indicate the start of the delimiter, so that the preceding portion of the string will be included following the last element.
The format string inside of the compound indicator should contain exactly one format specifier (two in case of associative arrays), which specifies the formatting mode of the elements of the compound type. This format specifier can be a compound indicator itself.

Note Inside a compound indicator, strings and characters are escaped automatically. To avoid this behavior, use "%-(" instead of "%(".

#### Flags

There are several flags that affect the outcome of the formatting.

Flag | Semantics
--- | ---
'-' | When the formatted result is shorter then the value given by the width parameter, the output is right justified. With the '-' flag this is changed to left justification. There are two exceptions where the '-' flag has a different meaning: (1) with 'r' it denotes to use little endian and (2) in case of a compound indicator it means that no special handling of the members is applied.
'=' | When the formatted result is shorter then the value given by the width parameter, the output is centered. If the central position is not possible it is moved slightly to the right. In this case, if '-' flag is present in addition to the '=' flag, it is moved slightly to the left.
'+' / ' ' | Applies to numerical values. By default, positive numbers are not formatted to include the + sign. With one of these two flags present, positive numbers are preceded by a plus sign or a space. When both flags are present, a plus sign is used. In case of 'r', a big endian format is used.
'0' | Is applied to numerical values that are printed right justified. If the zero flag is present, the space left to the number is filled with zeros instead of spaces.
'#' | Denotes that an alternative output must be used. This depends on the type to be formatted and the format character used. See the sections below for more information.

#### Width, Precision and Separator

The width parameter specifies the minimum width of the result.
The meaning of precision depends on the format indicator. For integers it denotes the minimum number of digits printed, for real numbers it denotes the number of fractional digits and for strings and compound types it denotes the maximum number of elements that are included in the output.
A separator is used for formatting numbers. If it is specified, the output is divided into chunks of three digits, separated by a ','. The number of digits in a chunk can be given explicitly by providing a number or a '*' after the ','.
In all three cases the number of digits can be replaced by a '*'. In this scenario, the next argument is used as the number of digits. If the argument is a negative number, the precision and separator parameters are considered unspecified. For width, the absolute value is used and the '-' flag is set.
The separator can also be followed by a '?'. In that case, an additional argument is used to specify the symbol that should be used to separate the chunks.

#### Position

By default, the arguments are processed in the provided order. With the position parameter it is possible to address arguments directly. It is also possible to denote a series of arguments with two numbers separated by ':', that are all processed in the same way. The second number can be omitted. In that case the series ends with the last argument.
It's also possible to use positional arguments for width, precision and separator by adding a number and a '$' after the '*'.

#### Types

This section describes the result of combining types with format characters. It is organized in 2 subsections: a list of general information regarding the formatting of types in the presence of format characters and a table that contains details for every available combination of type and format character.
When formatting types, the following rules apply:

- If the format character is upper case, the resulting string will be formatted using upper case letters.
- The default precision for floating point numbers is 6 digits.
- Rounding of floating point numbers adheres to the rounding mode of the floating point unit, if available.
- The floating point values NaN and Infinity are formatted as nan and inf, possibly preceded by '+' or '-' sign.
- Formatting reals is only supported for 64 bit reals and 80 bit reals. All other reals are cast to double before they are formatted. This will cause the result to be inf for very large numbers.
- Characters and strings formatted with the 's' format character inside of compound types are surrounded by single and double quotes and unprintable characters are escaped. To avoid this, a '-' flag can be specified for the compound specifier (e.g. "%-(%s%)" instead of "%(%s%)" ).
- Structs, unions, classes and interfaces are formatted by calling a toString method if available. See module std.format.write for more details.
- Only part of these combinations can be used for reading. See module std.format.read for more detailed information.

This table contains descriptions for every possible combination of type and format character:

Type | Format Character | Formatted as...
--- | --- | ---
null | 's' | null
bool | 's' | false or true
'b', 'd', 'o', 'u', 'x', 'X' | As the integrals 0 or 1 with the same format character. Please note, that 'o' and 'x' with '#' flag might produce unexpected results due to special handling of the value 0.
'r' | \0 or \1
Integral | 's', 'd' | A signed decimal number. The '#' flag is ignored.
'b', 'o', 'u', 'x', 'X' | An unsigned binary, decimal, octal or hexadecimal number. In case of 'o' and 'x', the '#' flag denotes that the number must be preceded by 0 and 0x, with the exception of the value 0, where this does not apply. For 'b' and 'u' the '#' flag has no effect.
'e', 'E', 'f', 'F', 'g', 'G', 'a', 'A' | As a floating point value with the same specifier. Default precision is large enough to add all digits of the integral value. In case of ($B 'a') and 'A', the integral digit can be any hexadecimal digit.
'r' | Characters taken directly from the binary representation.
Floating Point | 'e', 'E' | Scientific notation: Exactly one integral digit followed by a dot and fractional digits, followed by the exponent. The exponent is formatted as 'e' followed by a '+' or '-' sign, followed by at least two digits. When there are no fractional digits and the '#' flag is not present, the dot is omitted.
'f', 'F' | Natural notation: Integral digits followed by a dot and fractional digits. When there are no fractional digits and the '#' flag is not present, the dot is omitted. Please note: the difference between 'f' and 'F' is only visible for NaN and Infinity.
's', 'g', 'G' | Short notation: If the absolute value is larger than 10 ^^ precision or smaller than 0.0001, the scientific notation is used. If not, the natural notation is applied. In both cases precision denotes the count of all digits, including the integral digits. Trailing zeros (including a trailing dot) are removed. If '#' flag is present, trailing zeros are not removed.
'a', 'A' | Hexadecimal scientific notation: 0x followed by 1 (or 0 in case of value zero or denormalized number) followed by a dot, fractional digits in hexadecimal notation and an exponent. The exponent is build by p, followed by a sign and the exponent in decimal notation. When there are no fractional digits and the '#' flag is not present, the dot is omitted.
'r' | Characters taken directly from the binary representation.
Character | 's', 'c' | As the character. Inside of a compound indicator 's' is treated differently: The character is surrounded by single quotes and non printable characters are escaped. This can be avoided by preceding the compound indicator with a '-' flag (e.g. "%-(%s%)").
'b', 'd', 'o', 'u', 'x', 'X' | As the integral that represents the character.
'r' | Characters taken directly from the binary representation.
String | 's' | The sequence of characters that form the string. Inside of a compound indicator the string is surrounded by double quotes and non printable characters are escaped. This can be avoided by preceding the compound indicator with a '-' flag (e.g. "%-(%s%)").
'r' | The sequence of characters, each formatted with 'r'.
compound | As an array of characters.
Array | 's' | When the elements are characters, the array is formatted as a string. In all other cases the array is surrounded by square brackets and the elements are separated by a comma and a space. If the elements are strings, they are surrounded by double quotes and non printable characters are escaped.
'r' | The sequence of the elements, each formatted with 'r'.
compound | The sequence of the elements, each formatted according to the specifications given inside of the compound specifier.
Associative Array | 's' | As a sequence of the elements in unpredictable order. The output is surrounded by square brackets. The elements are separated by a comma and a space. The elements are formatted as key:value.
compound | As a sequence of the elements in unpredictable order. Each element is formatted according to the specifications given inside of the compound specifier. The first specifier is used for formatting the key and the second specifier is used for formatting the value. The order can be changed with positional arguments. For example "%(%2$s (%1$s), %)" will write the value, followed by the key in parenthesis.
Enum | 's' | The name of the value. If the name is not available, the base value is used, preceeded by a cast.
All, but 's' | Enums can be formatted with all format characters that can be used with the base value. In that case they are formatted like the base value.
Input Range | 's' | When the elements of the range are characters, they are written like a string. In all other cases, the elements are enclosed by square brackets and separated by a comma and a space.
'r' | The sequence of the elements, each formatted with 'r'.
compound | The sequence of the elements, each formatted according to the specifications given inside of the compound specifier.
Struct | 's' | When the struct has neither an applicable toString nor is an input range, it is formatted as follows: StructType(field1, field2, ...).
Class | 's' | When the class has neither an applicable toString nor is an input range, it is formatted as the fully qualified name of the class.
Union | 's' | When the union has neither an applicable toString nor is an input range, it is formatted as its base name.
Pointer | 's' | A null pointer is formatted as 'null'. All other pointers are formatted as hexadecimal numbers with the format character 'X'.
'x', 'X' | Formatted as a hexadecimal number.
SIMD vector | 's' | The array is surrounded by square brackets and the elements are separated by a comma and a space.
'r' | The sequence of the elements, each formatted with 'r'.
compound | The sequence of the elements, each formatted according to the specifications given inside of the compound specifier.
Delegate | 's', 'r', compound | As the .stringof of this delegate treated as a string. Please note: The implementation is currently buggy and its use is discouraged.

License:
Boost License 1.0.
Authors:
Walter Bright, Andrei Alexandrescu, and Kenji Hara

Source std/format.d

Examples:
Simple use:

    // Easiest way is to use `%s` everywhere:
    // "I got 30 eggs for 5.27 euros."
    writeln(format("I got %s %s for %s euros.", 30, "eggs", 5.27));

    // Other format characters provide more control:
    // "I got 11110 65676773 for 5.270000 euros."
    writeln(format("I got %b %(%X%) for %f euros.", 30, "eggs", 5.27));

Examples:
Compound specifiers allow formatting arrays and other compound types:

    /*
    The trailing end of the sub-format string following the specifier for
    each item is interpreted as the array delimiter, and is therefore
    omitted following the last array item:
     */
        writeln(format("My items are %(%s %).", [1, 2, 3])); // "My items are 1 2 3."
        writeln(format("My items are %(%s, %).", [1, 2, 3])); // "My items are 1, 2, 3."

    /*
    The "%|" delimiter specifier may be used to indicate where the
    delimiter begins, so that the portion of the format string prior to
    it will be retained in the last array element:
     */
        writeln(format("My items are %(-%s-%|, %).", [1, 2, 3])); // "My items are -1-, -2-, -3-."

    /*
    These compound format specifiers may be nested in the case of a
    nested array argument:
     */
        auto mat = [[1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9]];

        assert(format("%(%(%d %) - %)", mat), "1 2 3 - 4 5 6 - 7 8 9");
        assert(format("[%(%(%d %) - %)]", mat), "[1 2 3 - 4 5 6 - 7 8 9]");
        assert(format("[%([%(%d %)]%| - %)]", mat), "[1 2 3] - [4 5 6] - [7 8 9]");

    /*
    Strings and characters are escaped automatically inside compound
    format specifiers. To avoid this behavior, use "%-(" instead of "%(":
     */
        // `My friends are ["John", "Nancy"].`
        writeln(format("My friends are %s.", ["John", "Nancy"]));
        // `My friends are "John", "Nancy".`
        writeln(format("My friends are %(%s, %).", ["John", "Nancy"]));
        // `My friends are John, Nancy.`
        writeln(format("My friends are %-(%s, %).", ["John", "Nancy"]));

Examples:
Using parameters:

    // Flags can be used to influence to outcome:
    writeln(format("%g != %+#g", 3.14, 3.14)); // "3.14 != +3.14000"

    // Width and precision help to arrange the formatted result:
    writeln(format(">%10.2f<", 1234.56789)); // ">   1234.57<"

    // Numbers can be grouped:
    writeln(format("%,4d", int.max)); // "21,4748,3647"

    // It's possible to specify the position of an argument:
    writeln(format("%3$s %1$s", 3, 17, 5)); // "5 3"

Examples:
Providing parameters as arguments:

    // Width as argument
    writeln(format(">%*s<", 10, "abc")); // ">       abc<"

    // Precision as argument
    writeln(format(">%.*f<", 5, 123.2)); // ">123.20000<"

    // Grouping as argument
    writeln(format("%,*d", 1, int.max)); // "2,1,4,7,4,8,3,6,4,7"

    // Grouping separator as argument
    writeln(format("%,3?d", '_', int.max)); // "2_147_483_647"

    // All at once
    writeln(format("%*.*,*?d", 20, 15, 6, '/', int.max)); // "   000/002147/483647"

class `FormatException`: object.Exception;
  Signals an issue encountered while formatting.
  Examples:

      import std.exception : assertThrown;

      assertThrown!FormatException(format("%d", "foo"));

  pure nothrow @nogc @safe this();
    Generic constructor.
  pure nothrow @nogc @safe this(string `msg`, string `fn` = __FILE__, size_t `ln` = __LINE__, Throwable `next` = null);
    Creates a new instance of FormatException.
    Parameters:

    string `msg` | message of the exception
    string `fn` | file name of the file where the exception was created (optional)
    size_t `ln` | line number of the file where the exception was created (optional)
    Throwable `next` | for internal use, should always be null (optional)

immutable(Char)[] `format`(Char, Args...)(in Char[] `fmt`, Args `args`)
if (isSomeChar!Char);
typeof(fmt) `format`(alias fmt, Args...)(Args `args`)
if (isSomeString!(typeof(fmt)));
  Converts its arguments according to a format string into a string.
  The second version of `format` takes the format string as template argument. In this case, it is checked for consistency at compile-time and produces slightly faster code, because the length of the output buffer can be estimated in advance.
  Parameters:

  Char[] `fmt` | a format string
  Args `args` | a variadic list of arguments to be formatted
  Char | character type of `fmt`
  Args | a variadic list of types of the arguments

  Returns:
  The formatted string.
  Throws:
  A FormatException if formatting did not succeed.
  See Also:
  sformat for a variant, that tries to avoid garbage collection.
  Examples:

      writeln(format("Here are %d %s.", 3, "apples")); // "Here are 3 apples."

      writeln("Increase: %7.2f %%".format(17.4285)); // "Increase:   17.43 %"

  Examples:
  The format string can be checked at compile-time:

      auto s = format!"%s is %s"("Pi", 3.14);
      writeln(s); // "Pi is 3.14"

      // This line doesn't compile, because 3.14 cannot be formatted with %d:
      // s = format!"%s is %d"("Pi", 3.14);

char[] `sformat`(Char, Args...)(return scope char[] `buf`, scope const(Char)[] `fmt`, Args `args`);
char[] `sformat`(alias fmt, Args...)(char[] `buf`, Args `args`)
if (isSomeString!(typeof(fmt)));
  Converts its arguments according to a format string into a buffer. The buffer has to be large enough to hold the formatted string.
  The second version of `sformat` takes the format string as a template argument. In this case, it is checked for consistency at compile-time.
  Parameters:

  char[] `buf` | the buffer where the formatted string should go
  const(Char)[] `fmt` | a format string
  Args `args` | a variadic list of arguments to be formatted
  Char | character type of `fmt`
  Args | a variadic list of types of the arguments

  Returns:
  A slice of `buf` containing the formatted string.
  Throws:
  A RangeError if `buf` isn't large enough to hold the formatted string and a FormatException if formatting did not succeed.

  Note In theory this function should be @nogc. But with the current implementation there are some cases where allocations occur:

  - An exception is thrown.
  - A custom toString function of a compound type allocates.
  Examples:

      char[20] buf;
      writeln(sformat(buf[], "Here are %d %s.", 3, "apples")); // "Here are 3 apples."

      writeln(buf[].sformat("Increase: %7.2f %%", 17.4285)); // "Increase:   17.43 %"

  Examples:
  The format string can be checked at compile-time:

      char[20] buf;

      writeln(sformat!"Here are %d %s."(buf[], 3, "apples")); // "Here are 3 apples."

      // This line doesn't compile, because 3.14 cannot be formatted with %d:
      // writeln(sformat!"Here are %d %s."(buf[], 3.14, "apples"));

Copyright © 1999-2021 by the D Language Foundation | Page generated by Ddoc on Thu Jun 3 21:31:55 2021