1.. _syntax: 2 3******************** 4Format String Syntax 5******************** 6 7Formatting functions such as :ref:`fmt::format() <format>` and 8:ref:`fmt::print() <print>` use the same format string syntax described in this 9section. 10 11Format strings contain "replacement fields" surrounded by curly braces ``{}``. 12Anything that is not contained in braces is considered literal text, which is 13copied unchanged to the output. If you need to include a brace character in the 14literal text, it can be escaped by doubling: ``{{`` and ``}}``. 15 16The grammar for a replacement field is as follows: 17 18.. productionlist:: sf 19 replacement_field: "{" [`arg_id`] [":" (`format_spec` | `chrono_format_spec`)] "}" 20 arg_id: `integer` | `identifier` 21 integer: `digit`+ 22 digit: "0"..."9" 23 identifier: `id_start` `id_continue`* 24 id_start: "a"..."z" | "A"..."Z" | "_" 25 id_continue: `id_start` | `digit` 26 27In less formal terms, the replacement field can start with an *arg_id* 28that specifies the argument whose value is to be formatted and inserted into 29the output instead of the replacement field. 30The *arg_id* is optionally followed by a *format_spec*, which is preceded by a 31colon ``':'``. These specify a non-default format for the replacement value. 32 33See also the :ref:`formatspec` section. 34 35If the numerical arg_ids in a format string are 0, 1, 2, ... in sequence, 36they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be 37automatically inserted in that order. 38 39Named arguments can be referred to by their names or indices. 40 41Some simple format string examples:: 42 43 "First, thou shalt count to {0}" // References the first argument 44 "Bring me a {}" // Implicitly references the first argument 45 "From {} to {}" // Same as "From {0} to {1}" 46 47The *format_spec* field contains a specification of how the value should be 48presented, including such details as field width, alignment, padding, decimal 49precision and so on. Each value type can define its own "formatting 50mini-language" or interpretation of the *format_spec*. 51 52Most built-in types support a common formatting mini-language, which is 53described in the next section. 54 55A *format_spec* field can also include nested replacement fields in certain 56positions within it. These nested replacement fields can contain only an 57argument id; format specifications are not allowed. This allows the formatting 58of a value to be dynamically specified. 59 60See the :ref:`formatexamples` section for some examples. 61 62.. _formatspec: 63 64Format Specification Mini-Language 65================================== 66 67"Format specifications" are used within replacement fields contained within a 68format string to define how individual values are presented (see 69:ref:`syntax`). Each formattable type may define how the format 70specification is to be interpreted. 71 72Most built-in types implement the following options for format specifications, 73although some of the formatting options are only supported by the numeric types. 74 75The general form of a *standard format specifier* is: 76 77.. productionlist:: sf 78 format_spec: [[`fill`]`align`][`sign`]["#"]["0"][`width`]["." `precision`]["L"][`type`] 79 fill: <a character other than '{' or '}'> 80 align: "<" | ">" | "^" 81 sign: "+" | "-" | " " 82 width: `integer` | "{" [`arg_id`] "}" 83 precision: `integer` | "{" [`arg_id`] "}" 84 type: "a" | "A" | "b" | "B" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | 85 : "o" | "p" | "s" | "x" | "X" | "?" 86 87The *fill* character can be any Unicode code point other than ``'{'`` or 88``'}'``. The presence of a fill character is signaled by the character following 89it, which must be one of the alignment options. If the second character of 90*format_spec* is not a valid alignment option, then it is assumed that both the 91fill character and the alignment option are absent. 92 93The meaning of the various alignment options is as follows: 94 95+---------+----------------------------------------------------------+ 96| Option | Meaning | 97+=========+==========================================================+ 98| ``'<'`` | Forces the field to be left-aligned within the available | 99| | space (this is the default for most objects). | 100+---------+----------------------------------------------------------+ 101| ``'>'`` | Forces the field to be right-aligned within the | 102| | available space (this is the default for numbers). | 103+---------+----------------------------------------------------------+ 104| ``'^'`` | Forces the field to be centered within the available | 105| | space. | 106+---------+----------------------------------------------------------+ 107 108Note that unless a minimum field width is defined, the field width will always 109be the same size as the data to fill it, so that the alignment option has no 110meaning in this case. 111 112The *sign* option is only valid for floating point and signed integer types, 113and can be one of the following: 114 115+---------+------------------------------------------------------------+ 116| Option | Meaning | 117+=========+============================================================+ 118| ``'+'`` | indicates that a sign should be used for both | 119| | nonnegative as well as negative numbers. | 120+---------+------------------------------------------------------------+ 121| ``'-'`` | indicates that a sign should be used only for negative | 122| | numbers (this is the default behavior). | 123+---------+------------------------------------------------------------+ 124| space | indicates that a leading space should be used on | 125| | nonnegative numbers, and a minus sign on negative numbers. | 126+---------+------------------------------------------------------------+ 127 128The ``'#'`` option causes the "alternate form" to be used for the 129conversion. The alternate form is defined differently for different 130types. This option is only valid for integer and floating-point types. 131For integers, when binary, octal, or hexadecimal output is used, this 132option adds the prefix respective ``"0b"`` (``"0B"``), ``"0"``, or 133``"0x"`` (``"0X"``) to the output value. Whether the prefix is 134lower-case or upper-case is determined by the case of the type 135specifier, for example, the prefix ``"0x"`` is used for the type ``'x'`` 136and ``"0X"`` is used for ``'X'``. For floating-point numbers the 137alternate form causes the result of the conversion to always contain a 138decimal-point character, even if no digits follow it. Normally, a 139decimal-point character appears in the result of these conversions 140only if a digit follows it. In addition, for ``'g'`` and ``'G'`` 141conversions, trailing zeros are not removed from the result. 142 143.. ifconfig:: False 144 145 The ``','`` option signals the use of a comma for a thousands separator. 146 For a locale aware separator, use the ``'L'`` integer presentation type 147 instead. 148 149*width* is a decimal integer defining the minimum field width. If not 150specified, then the field width will be determined by the content. 151 152Preceding the *width* field by a zero (``'0'``) character enables sign-aware 153zero-padding for numeric types. It forces the padding to be placed after the 154sign or base (if any) but before the digits. This is used for printing fields in 155the form '+000000120'. This option is only valid for numeric types and it has no 156effect on formatting of infinity and NaN. 157 158The *precision* is a decimal number indicating how many digits should be 159displayed after the decimal point for a floating-point value formatted with 160``'f'`` and ``'F'``, or before and after the decimal point for a floating-point 161value formatted with ``'g'`` or ``'G'``. For non-number types the field 162indicates the maximum field size - in other words, how many characters will be 163used from the field content. The *precision* is not allowed for integer, 164character, Boolean, and pointer values. Note that a C string must be 165null-terminated even if precision is specified. 166 167The ``'L'`` option uses the current locale setting to insert the appropriate 168number separator characters. This option is only valid for numeric types. 169 170Finally, the *type* determines how the data should be presented. 171 172The available string presentation types are: 173 174+---------+----------------------------------------------------------+ 175| Type | Meaning | 176+=========+==========================================================+ 177| ``'s'`` | String format. This is the default type for strings and | 178| | may be omitted. | 179+---------+----------------------------------------------------------+ 180| ``'?'`` | Debug format. The string is quoted and special | 181| | characters escaped. | 182+---------+----------------------------------------------------------+ 183| none | The same as ``'s'``. | 184+---------+----------------------------------------------------------+ 185 186The available character presentation types are: 187 188+---------+----------------------------------------------------------+ 189| Type | Meaning | 190+=========+==========================================================+ 191| ``'c'`` | Character format. This is the default type for | 192| | characters and may be omitted. | 193+---------+----------------------------------------------------------+ 194| ``'?'`` | Debug format. The character is quoted and special | 195| | characters escaped. | 196+---------+----------------------------------------------------------+ 197| none | The same as ``'c'``. | 198+---------+----------------------------------------------------------+ 199 200The available integer presentation types are: 201 202+---------+----------------------------------------------------------+ 203| Type | Meaning | 204+=========+==========================================================+ 205| ``'b'`` | Binary format. Outputs the number in base 2. Using the | 206| | ``'#'`` option with this type adds the prefix ``"0b"`` | 207| | to the output value. | 208+---------+----------------------------------------------------------+ 209| ``'B'`` | Binary format. Outputs the number in base 2. Using the | 210| | ``'#'`` option with this type adds the prefix ``"0B"`` | 211| | to the output value. | 212+---------+----------------------------------------------------------+ 213| ``'c'`` | Character format. Outputs the number as a character. | 214+---------+----------------------------------------------------------+ 215| ``'d'`` | Decimal integer. Outputs the number in base 10. | 216+---------+----------------------------------------------------------+ 217| ``'o'`` | Octal format. Outputs the number in base 8. | 218+---------+----------------------------------------------------------+ 219| ``'x'`` | Hex format. Outputs the number in base 16, using | 220| | lower-case letters for the digits above 9. Using the | 221| | ``'#'`` option with this type adds the prefix ``"0x"`` | 222| | to the output value. | 223+---------+----------------------------------------------------------+ 224| ``'X'`` | Hex format. Outputs the number in base 16, using | 225| | upper-case letters for the digits above 9. Using the | 226| | ``'#'`` option with this type adds the prefix ``"0X"`` | 227| | to the output value. | 228+---------+----------------------------------------------------------+ 229| none | The same as ``'d'``. | 230+---------+----------------------------------------------------------+ 231 232Integer presentation types can also be used with character and Boolean values 233with the only exception that ``'c'`` cannot be used with `bool`. Boolean values 234are formatted using textual representation, either ``true`` or ``false``, if the 235presentation type is not specified. 236 237The available presentation types for floating-point values are: 238 239+---------+----------------------------------------------------------+ 240| Type | Meaning | 241+=========+==========================================================+ 242| ``'a'`` | Hexadecimal floating point format. Prints the number in | 243| | base 16 with prefix ``"0x"`` and lower-case letters for | 244| | digits above 9. Uses ``'p'`` to indicate the exponent. | 245+---------+----------------------------------------------------------+ 246| ``'A'`` | Same as ``'a'`` except it uses upper-case letters for | 247| | the prefix, digits above 9 and to indicate the exponent. | 248+---------+----------------------------------------------------------+ 249| ``'e'`` | Exponent notation. Prints the number in scientific | 250| | notation using the letter 'e' to indicate the exponent. | 251+---------+----------------------------------------------------------+ 252| ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an | 253| | upper-case ``'E'`` as the separator character. | 254+---------+----------------------------------------------------------+ 255| ``'f'`` | Fixed point. Displays the number as a fixed-point | 256| | number. | 257+---------+----------------------------------------------------------+ 258| ``'F'`` | Fixed point. Same as ``'f'``, but converts ``nan`` to | 259| | ``NAN`` and ``inf`` to ``INF``. | 260+---------+----------------------------------------------------------+ 261| ``'g'`` | General format. For a given precision ``p >= 1``, | 262| | this rounds the number to ``p`` significant digits and | 263| | then formats the result in either fixed-point format | 264| | or in scientific notation, depending on its magnitude. | 265| | | 266| | A precision of ``0`` is treated as equivalent to a | 267| | precision of ``1``. | 268+---------+----------------------------------------------------------+ 269| ``'G'`` | General format. Same as ``'g'`` except switches to | 270| | ``'E'`` if the number gets too large. The | 271| | representations of infinity and NaN are uppercased, too. | 272+---------+----------------------------------------------------------+ 273| none | Similar to ``'g'``, except that the default precision is | 274| | as high as needed to represent the particular value. | 275+---------+----------------------------------------------------------+ 276 277.. ifconfig:: False 278 279 +---------+----------------------------------------------------------+ 280 | | The precise rules are as follows: suppose that the | 281 | | result formatted with presentation type ``'e'`` and | 282 | | precision ``p-1`` would have exponent ``exp``. Then | 283 | | if ``-4 <= exp < p``, the number is formatted | 284 | | with presentation type ``'f'`` and precision | 285 | | ``p-1-exp``. Otherwise, the number is formatted | 286 | | with presentation type ``'e'`` and precision ``p-1``. | 287 | | In both cases insignificant trailing zeros are removed | 288 | | from the significand, and the decimal point is also | 289 | | removed if there are no remaining digits following it. | 290 | | | 291 | | Positive and negative infinity, positive and negative | 292 | | zero, and nans, are formatted as ``inf``, ``-inf``, | 293 | | ``0``, ``-0`` and ``nan`` respectively, regardless of | 294 | | the precision. | 295 | | | 296 +---------+----------------------------------------------------------+ 297 298The available presentation types for pointers are: 299 300+---------+----------------------------------------------------------+ 301| Type | Meaning | 302+=========+==========================================================+ 303| ``'p'`` | Pointer format. This is the default type for | 304| | pointers and may be omitted. | 305+---------+----------------------------------------------------------+ 306| none | The same as ``'p'``. | 307+---------+----------------------------------------------------------+ 308 309.. _chrono-specs: 310 311Chrono Format Specifications 312============================ 313 314Format specifications for chrono duration and time point types as well as 315``std::tm`` have the following syntax: 316 317.. productionlist:: sf 318 chrono_format_spec: [[`fill`]`align`][`width`]["." `precision`][`chrono_specs`] 319 chrono_specs: [`chrono_specs`] `conversion_spec` | `chrono_specs` `literal_char` 320 conversion_spec: "%" [`modifier`] `chrono_type` 321 literal_char: <a character other than '{', '}' or '%'> 322 modifier: "E" | "O" 323 chrono_type: "a" | "A" | "b" | "B" | "c" | "C" | "d" | "D" | "e" | "F" | 324 : "g" | "G" | "h" | "H" | "I" | "j" | "m" | "M" | "n" | "p" | 325 : "q" | "Q" | "r" | "R" | "S" | "t" | "T" | "u" | "U" | "V" | 326 : "w" | "W" | "x" | "X" | "y" | "Y" | "z" | "Z" | "%" 327 328Literal chars are copied unchanged to the output. Precision is valid only for 329``std::chrono::duration`` types with a floating-point representation type. 330 331The available presentation types (*chrono_type*) are: 332 333+---------+--------------------------------------------------------------------+ 334| Type | Meaning | 335+=========+====================================================================+ 336| ``'a'`` | The abbreviated weekday name, e.g. "Sat". If the value does not | 337| | contain a valid weekday, an exception of type ``format_error`` is | 338| | thrown. | 339+---------+--------------------------------------------------------------------+ 340| ``'A'`` | The full weekday name, e.g. "Saturday". If the value does not | 341| | contain a valid weekday, an exception of type ``format_error`` is | 342| | thrown. | 343+---------+--------------------------------------------------------------------+ 344| ``'b'`` | The abbreviated month name, e.g. "Nov". If the value does not | 345| | contain a valid month, an exception of type ``format_error`` is | 346| | thrown. | 347+---------+--------------------------------------------------------------------+ 348| ``'B'`` | The full month name, e.g. "November". If the value does not | 349| | contain a valid month, an exception of type ``format_error`` is | 350| | thrown. | 351+---------+--------------------------------------------------------------------+ 352| ``'c'`` | The date and time representation, e.g. "Sat Nov 12 22:04:00 1955". | 353| | The modified command ``%Ec`` produces the locale's alternate date | 354| | and time representation. | 355+---------+--------------------------------------------------------------------+ 356| ``'C'`` | The year divided by 100 using floored division, e.g. "55". If the | 357| | result is a single decimal digit, it is prefixed with 0. | 358| | The modified command ``%EC`` produces the locale's alternative | 359| | representation of the century. | 360+---------+--------------------------------------------------------------------+ 361| ``'d'`` | The day of month as a decimal number. If the result is a single | 362| | decimal digit, it is prefixed with 0. The modified command ``%Od`` | 363| | produces the locale's alternative representation. | 364+---------+--------------------------------------------------------------------+ 365| ``'D'`` | Equivalent to ``%m/%d/%y``, e.g. "11/12/55". | 366+---------+--------------------------------------------------------------------+ 367| ``'e'`` | The day of month as a decimal number. If the result is a single | 368| | decimal digit, it is prefixed with a space. The modified command | 369| | ``%Oe`` produces the locale's alternative representation. | 370+---------+--------------------------------------------------------------------+ 371| ``'F'`` | Equivalent to ``%Y-%m-%d``, e.g. "1955-11-12". | 372+---------+--------------------------------------------------------------------+ 373| ``'g'`` | The last two decimal digits of the ISO week-based year. If the | 374| | result is a single digit it is prefixed by 0. | 375+---------+--------------------------------------------------------------------+ 376| ``'G'`` | The ISO week-based year as a decimal number. If the result is less | 377| | than four digits it is left-padded with 0 to four digits. | 378+---------+--------------------------------------------------------------------+ 379| ``'h'`` | Equivalent to ``%b``, e.g. "Nov". | 380+---------+--------------------------------------------------------------------+ 381| ``'H'`` | The hour (24-hour clock) as a decimal number. If the result is a | 382| | single digit, it is prefixed with 0. The modified command ``%OH`` | 383| | produces the locale's alternative representation. | 384+---------+--------------------------------------------------------------------+ 385| ``'I'`` | The hour (12-hour clock) as a decimal number. If the result is a | 386| | single digit, it is prefixed with 0. The modified command ``%OI`` | 387| | produces the locale's alternative representation. | 388+---------+--------------------------------------------------------------------+ 389| ``'j'`` | If the type being formatted is a specialization of duration, the | 390| | decimal number of days without padding. Otherwise, the day of the | 391| | year as a decimal number. Jan 1 is 001. If the result is less than | 392| | three digits, it is left-padded with 0 to three digits. | 393+---------+--------------------------------------------------------------------+ 394| ``'m'`` | The month as a decimal number. Jan is 01. If the result is a | 395| | single digit, it is prefixed with 0. The modified command ``%Om`` | 396| | produces the locale's alternative representation. | 397+---------+--------------------------------------------------------------------+ 398| ``'M'`` | The minute as a decimal number. If the result is a single digit, | 399| | it is prefixed with 0. The modified command ``%OM`` produces the | 400| | locale's alternative representation. | 401+---------+--------------------------------------------------------------------+ 402| ``'n'`` | A new-line character. | 403+---------+--------------------------------------------------------------------+ 404| ``'p'`` | The AM/PM designations associated with a 12-hour clock. | 405+---------+--------------------------------------------------------------------+ 406| ``'q'`` | The duration's unit suffix. | 407+---------+--------------------------------------------------------------------+ 408| ``'Q'`` | The duration's numeric value (as if extracted via ``.count()``). | 409+---------+--------------------------------------------------------------------+ 410| ``'r'`` | The 12-hour clock time, e.g. "10:04:00 PM". | 411+---------+--------------------------------------------------------------------+ 412| ``'R'`` | Equivalent to ``%H:%M``, e.g. "22:04". | 413+---------+--------------------------------------------------------------------+ 414| ``'S'`` | Seconds as a decimal number. If the number of seconds is less than | 415| | 10, the result is prefixed with 0. If the precision of the input | 416| | cannot be exactly represented with seconds, then the format is a | 417| | decimal floating-point number with a fixed format and a precision | 418| | matching that of the precision of the input (or to a microseconds | 419| | precision if the conversion to floating-point decimal seconds | 420| | cannot be made within 18 fractional digits). The character for the | 421| | decimal point is localized according to the locale. The modified | 422| | command ``%OS`` produces the locale's alternative representation. | 423+---------+--------------------------------------------------------------------+ 424| ``'t'`` | A horizontal-tab character. | 425+---------+--------------------------------------------------------------------+ 426| ``'T'`` | Equivalent to ``%H:%M:%S``. | 427+---------+--------------------------------------------------------------------+ 428| ``'u'`` | The ISO weekday as a decimal number (1-7), where Monday is 1. The | 429| | modified command ``%Ou`` produces the locale's alternative | 430| | representation. | 431+---------+--------------------------------------------------------------------+ 432| ``'U'`` | The week number of the year as a decimal number. The first Sunday | 433| | of the year is the first day of week 01. Days of the same year | 434| | prior to that are in week 00. If the result is a single digit, it | 435| | is prefixed with 0. The modified command ``%OU`` produces the | 436| | locale's alternative representation. | 437+---------+--------------------------------------------------------------------+ 438| ``'V'`` | The ISO week-based week number as a decimal number. If the result | 439| | is a single digit, it is prefixed with 0. The modified command | 440| | ``%OV`` produces the locale's alternative representation. | 441+---------+--------------------------------------------------------------------+ 442| ``'w'`` | The weekday as a decimal number (0-6), where Sunday is 0. | 443| | The modified command ``%Ow`` produces the locale's alternative | 444| | representation. | 445+---------+--------------------------------------------------------------------+ 446| ``'W'`` | The week number of the year as a decimal number. The first Monday | 447| | of the year is the first day of week 01. Days of the same year | 448| | prior to that are in week 00. If the result is a single digit, it | 449| | is prefixed with 0. The modified command ``%OW`` produces the | 450| | locale's alternative representation. | 451+---------+--------------------------------------------------------------------+ 452| ``'x'`` | The date representation, e.g. "11/12/55". The modified command | 453| | ``%Ex`` produces the locale's alternate date representation. | 454+---------+--------------------------------------------------------------------+ 455| ``'X'`` | The time representation, e.g. "10:04:00". The modified command | 456| | ``%EX`` produces the locale's alternate time representation. | 457+---------+--------------------------------------------------------------------+ 458| ``'y'`` | The last two decimal digits of the year. If the result is a single | 459| | digit it is prefixed by 0. The modified command ``%Oy`` produces | 460| | the locale's alternative representation. The modified command | 461| | ``%Ey`` produces the locale's alternative representation of offset | 462| | from ``%EC`` (year only). | 463+---------+--------------------------------------------------------------------+ 464| ``'Y'`` | The year as a decimal number. If the result is less than four | 465| | digits it is left-padded with 0 to four digits. The modified | 466| | command ``%EY`` produces the locale's alternative full year | 467| | representation. | 468+---------+--------------------------------------------------------------------+ 469| ``'z'`` | The offset from UTC in the ISO 8601:2004 format. For example -0430 | 470| | refers to 4 hours 30 minutes behind UTC. If the offset is zero, | 471| | +0000 is used. The modified commands ``%Ez`` and ``%Oz`` insert a | 472| | ``:`` between the hours and minutes: -04:30. If the offset | 473| | information is not available, an exception of type | 474| | ``format_error`` is thrown. | 475+---------+--------------------------------------------------------------------+ 476| ``'Z'`` | The time zone abbreviation. If the time zone abbreviation is not | 477| | available, an exception of type ``format_error`` is thrown. | 478+---------+--------------------------------------------------------------------+ 479| ``'%'`` | A % character. | 480+---------+--------------------------------------------------------------------+ 481 482Specifiers that have a calendaric component such as ``'d'`` (the day of month) 483are valid only for ``std::tm`` and time points but not durations. 484 485.. range-specs: 486 487Range Format Specifications 488=========================== 489 490Format specifications for range types have the following syntax: 491 492.. productionlist:: sf 493 range_format_spec: [":" [`underlying_spec`]] 494 495The `underlying_spec` is parsed based on the formatter of the range's 496reference type. 497 498By default, a range of characters or strings is printed escaped and quoted. But 499if any `underlying_spec` is provided (even if it is empty), then the characters 500or strings are printed according to the provided specification. 501 502Examples:: 503 504 fmt::format("{}", std::vector{10, 20, 30}); 505 // Result: [10, 20, 30] 506 fmt::format("{::#x}", std::vector{10, 20, 30}); 507 // Result: [0xa, 0x14, 0x1e] 508 fmt::format("{}", vector{'h', 'e', 'l', 'l', 'o'}); 509 // Result: ['h', 'e', 'l', 'l', 'o'] 510 fmt::format("{::}", vector{'h', 'e', 'l', 'l', 'o'}); 511 // Result: [h, e, l, l, o] 512 fmt::format("{::d}", vector{'h', 'e', 'l', 'l', 'o'}); 513 // Result: [104, 101, 108, 108, 111] 514 515.. _formatexamples: 516 517Format Examples 518=============== 519 520This section contains examples of the format syntax and comparison with 521the printf formatting. 522 523In most of the cases the syntax is similar to the printf formatting, with the 524addition of the ``{}`` and with ``:`` used instead of ``%``. 525For example, ``"%03.2f"`` can be translated to ``"{:03.2f}"``. 526 527The new format syntax also supports new and different options, shown in the 528following examples. 529 530Accessing arguments by position:: 531 532 fmt::format("{0}, {1}, {2}", 'a', 'b', 'c'); 533 // Result: "a, b, c" 534 fmt::format("{}, {}, {}", 'a', 'b', 'c'); 535 // Result: "a, b, c" 536 fmt::format("{2}, {1}, {0}", 'a', 'b', 'c'); 537 // Result: "c, b, a" 538 fmt::format("{0}{1}{0}", "abra", "cad"); // arguments' indices can be repeated 539 // Result: "abracadabra" 540 541Aligning the text and specifying a width:: 542 543 fmt::format("{:<30}", "left aligned"); 544 // Result: "left aligned " 545 fmt::format("{:>30}", "right aligned"); 546 // Result: " right aligned" 547 fmt::format("{:^30}", "centered"); 548 // Result: " centered " 549 fmt::format("{:*^30}", "centered"); // use '*' as a fill char 550 // Result: "***********centered***********" 551 552Dynamic width:: 553 554 fmt::format("{:<{}}", "left aligned", 30); 555 // Result: "left aligned " 556 557Dynamic precision:: 558 559 fmt::format("{:.{}f}", 3.14, 1); 560 // Result: "3.1" 561 562Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign:: 563 564 fmt::format("{:+f}; {:+f}", 3.14, -3.14); // show it always 565 // Result: "+3.140000; -3.140000" 566 fmt::format("{: f}; {: f}", 3.14, -3.14); // show a space for positive numbers 567 // Result: " 3.140000; -3.140000" 568 fmt::format("{:-f}; {:-f}", 3.14, -3.14); // show only the minus -- same as '{:f}; {:f}' 569 // Result: "3.140000; -3.140000" 570 571Replacing ``%x`` and ``%o`` and converting the value to different bases:: 572 573 fmt::format("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); 574 // Result: "int: 42; hex: 2a; oct: 52; bin: 101010" 575 // with 0x or 0 or 0b as prefix: 576 fmt::format("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}", 42); 577 // Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010" 578 579Padded hex byte with prefix and always prints both hex characters:: 580 581 fmt::format("{:#04x}", 0); 582 // Result: "0x00" 583 584Box drawing using Unicode fill:: 585 586 fmt::print( 587 "┌{0:─^{2}}┐\n" 588 "│{1: ^{2}}│\n" 589 "└{0:─^{2}}┘\n", "", "Hello, world!", 20); 590 591prints:: 592 593 ┌────────────────────┐ 594 │ Hello, world! │ 595 └────────────────────┘ 596 597Using type-specific formatting:: 598 599 #include <fmt/chrono.h> 600 601 auto t = tm(); 602 t.tm_year = 2010 - 1900; 603 t.tm_mon = 7; 604 t.tm_mday = 4; 605 t.tm_hour = 12; 606 t.tm_min = 15; 607 t.tm_sec = 58; 608 fmt::print("{:%Y-%m-%d %H:%M:%S}", t); 609 // Prints: 2010-08-04 12:15:58 610 611Using the comma as a thousands separator:: 612 613 #include <fmt/format.h> 614 615 auto s = fmt::format(std::locale("en_US.UTF-8"), "{:L}", 1234567890); 616 // s == "1,234,567,890" 617 618.. ifconfig:: False 619 620 Nesting arguments and more complex examples:: 621 622 >>> for align, text in zip('<^>', ['left', 'center', 'right']): 623 ... '{0:{fill}{align}16}") << text, fill=align, align=align) 624 ... 625 'left<<<<<<<<<<<<' 626 '^^^^^center^^^^^' 627 '>>>>>>>>>>>right' 628 >>> 629 >>> octets = [192, 168, 0, 1] 630 Format("{:02X}{:02X}{:02X}{:02X}") << *octets) 631 'C0A80001' 632 >>> int(_, 16) 633 3232235521 634 >>> 635 >>> width = 5 636 >>> for num in range(5,12): 637 ... for base in 'dXob': 638 ... print('{0:{width}{base}}") << num, base=base, width=width), end=' ') 639 ... print() 640 ... 641 5 5 5 101 642 6 6 6 110 643 7 7 7 111 644 8 8 10 1000 645 9 9 11 1001 646 10 A 12 1010 647 11 B 13 1011 648