CINXE.COM
Syntax - {fmt}
<!doctype html> <html lang="en" class="no-js"> <head> <base href="https://fmt.dev/latest/syntax/"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <link rel="prev" href="../api/"> <link rel="icon" href="../assets/images/favicon.png"> <meta name="generator" content="mkdocs-1.6.0, mkdocs-material-9.5.25"> <title>Syntax - {fmt}</title> <link rel="stylesheet" href="../assets/stylesheets/main.6543a935.min.css"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback"> <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style> <link rel="stylesheet" href="../assets/_mkdocstrings.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/styles/default.min.css"> <link rel="stylesheet" href="../fmt.css"> <script>__md_scope=new URL("..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script> <meta http-equiv="X-Translated-By" content="Google"> <meta http-equiv="X-Translated-To" content="iw"> <script type="text/javascript" src="https://www.gstatic.com/_/translate_http/_/js/k=translate_http.tr.en_GB.SlmlLUpNCG8.O/am=gAE/d=1/rs=AN8SPfrVDwgQMj0EYXdUQwFqbkL3aXptKg/m=corsproxy" data-sourceurl="https://fmt.dev/latest/syntax/"></script> <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" rel="stylesheet"> <script type="text/javascript" src="https://www.gstatic.com/_/translate_http/_/js/k=translate_http.tr.en_GB.SlmlLUpNCG8.O/am=gAE/d=1/exm=corsproxy/ed=1/rs=AN8SPfrVDwgQMj0EYXdUQwFqbkL3aXptKg/m=phishing_protection" data-phishing-protection-enabled="false" data-forms-warning-enabled="true" data-source-url="https://fmt.dev/latest/syntax/"></script> <meta name="robots" content="none"> </head> <body dir="ltr"> <script type="text/javascript" src="https://www.gstatic.com/_/translate_http/_/js/k=translate_http.tr.en_GB.SlmlLUpNCG8.O/am=gAE/d=1/exm=corsproxy,phishing_protection/ed=1/rs=AN8SPfrVDwgQMj0EYXdUQwFqbkL3aXptKg/m=navigationui" data-environment="prod" data-proxy-url="https://fmt-dev.translate.goog" data-proxy-full-url="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" data-source-url="https://fmt.dev/latest/syntax/" data-source-language="pl" data-target-language="iw" data-display-language="en-GB" data-detected-source-language="" data-is-source-untranslated="false" data-source-untranslated-url="https://translate.google.com/website?sl=pl&tl=iw&hl=en-GB&u=https://fmt.dev/latest/syntax/&anno=2" data-client="tr"></script><input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off"> <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off"> <label class="md-overlay" for="__drawer"></label> <div data-md-component="skip"><a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-string-syntax" class="md-skip"> Skip to content </a> </div> <div data-md-component="announce"> </div> <div data-md-color-scheme="default" data-md-component="outdated" hidden> </div> <header class="md-header" data-md-component="header"> <nav class="md-header__inner md-grid" aria-label="Header"><a href="https://fmt-dev.translate.goog/latest/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" title="{fmt}" class="md-header__button md-logo" aria-label="{fmt}" data-md-component="logo"> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z" /> </svg></a> <label class="md-header__button md-icon" for="__drawer"> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z" /> </svg></label> <div class="md-header__title" data-md-component="header-title"> <div class="md-header__ellipsis"> <div class="md-header__topic"><span class="md-ellipsis"> {fmt} </span> </div> <div class="md-header__topic" data-md-component="header-topic"><span class="md-ellipsis"> Syntax </span> </div> </div> </div> <script>var media,input,key,value,palette=__md_get("__palette");if(palette&&palette.color){"(prefers-color-scheme)"===palette.color.media&&(media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']"),palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent"));for([key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script><label class="md-header__button md-icon" for="__search"> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z" /> </svg></label> <div class="md-search" data-md-component="search" role="dialog"><label class="md-search__overlay" for="__search"></label> <div class="md-search__inner" role="search"> <form class="md-search__form" name="search"><input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required> <label class="md-search__icon md-icon" for="__search"> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z" /> </svg> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z" /> </svg></label> <nav class="md-search__options" aria-label="Search"><button type="reset" class="md-search__icon md-icon" title="Clear" aria-label="Clear" tabindex="-1"> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z" /> </svg></button> </nav> </form> <div class="md-search__output"> <div class="md-search__scrollwrap" data-md-scrollfix> <div class="md-search-result" data-md-component="search-result"> <div class="md-search-result__meta"> Initializing search </div> <ol class="md-search-result__list" role="presentation"></ol> </div> </div> </div> </div> </div> <div class="md-header__source"><a href="https://translate.google.com/website?sl=pl&tl=iw&hl=en-GB&u=https://github.com/fmtlib/fmt" title="Go to repository" class="md-source" data-md-component="source"> <div class="md-source__icon md-icon"> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 448 512"> <!--! Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z" /> </svg> </div> <div class="md-source__repository"> GitHub </div></a> </div> </nav> </header> <div class="md-container" data-md-component="container"> <nav class="md-tabs" aria-label="Tabs" data-md-component="tabs"> <div class="md-grid"> <ul class="md-tabs__list"> <li class="md-tabs__item"><a href="https://fmt-dev.translate.goog/latest/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" class="md-tabs__link"> Home </a></li> <li class="md-tabs__item"><a href="https://fmt-dev.translate.goog/latest/get-started/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" class="md-tabs__link"> Get Started </a></li> <li class="md-tabs__item"><a href="https://fmt-dev.translate.goog/latest/api/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" class="md-tabs__link"> API </a></li> <li class="md-tabs__item md-tabs__item--active"><a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" class="md-tabs__link"> Syntax </a></li> </ul> </div> </nav> <main class="md-main" data-md-component="main"> <div class="md-main__inner md-grid"> <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation"> <div class="md-sidebar__scrollwrap"> <div class="md-sidebar__inner"> <nav class="md-nav md-nav--primary md-nav--lifted md-nav--integrated" aria-label="Navigation" data-md-level="0"><label class="md-nav__title" for="__drawer"> <a href="https://fmt-dev.translate.goog/latest/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" title="{fmt}" class="md-nav__button md-logo" aria-label="{fmt}" data-md-component="logo"> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54Z" /> </svg></a> {fmt} </label> <div class="md-nav__source"><a href="https://translate.google.com/website?sl=pl&tl=iw&hl=en-GB&u=https://github.com/fmtlib/fmt" title="Go to repository" class="md-source" data-md-component="source"> <div class="md-source__icon md-icon"> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 448 512"> <!--! Font Awesome Free 6.5.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z" /> </svg> </div> <div class="md-source__repository"> GitHub </div></a> </div> <ul class="md-nav__list" data-md-scrollfix> <li class="md-nav__item"><a href="https://fmt-dev.translate.goog/latest/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" class="md-nav__link"> <span class="md-ellipsis"> Home </span> </a></li> <li class="md-nav__item"><a href="https://fmt-dev.translate.goog/latest/get-started/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" class="md-nav__link"> <span class="md-ellipsis"> Get Started </span> </a></li> <li class="md-nav__item"><a href="https://fmt-dev.translate.goog/latest/api/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" class="md-nav__link"> <span class="md-ellipsis"> API </span> </a></li> <li class="md-nav__item md-nav__item--active"><input class="md-nav__toggle md-toggle" type="checkbox" id="__toc"> <label class="md-nav__link md-nav__link--active" for="__toc"> <span class="md-ellipsis"> Syntax </span> <span class="md-nav__icon md-icon"></span> </label> <a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB" class="md-nav__link md-nav__link--active"> <span class="md-ellipsis"> Syntax </span> </a> <nav class="md-nav md-nav--secondary" aria-label="Table of contents"><label class="md-nav__title" for="__toc"> <span class="md-nav__icon md-icon"></span> Table of contents </label> <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix> <li class="md-nav__item"><a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-specification-mini-language" class="md-nav__link"> <span class="md-ellipsis"> Format Specification Mini-Language </span> </a></li> <li class="md-nav__item"><a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#chrono-format-specifications" class="md-nav__link"> <span class="md-ellipsis"> Chrono Format Specifications </span> </a></li> <li class="md-nav__item"><a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#range-format-specifications" class="md-nav__link"> <span class="md-ellipsis"> Range Format Specifications </span> </a></li> <li class="md-nav__item"><a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-examples" class="md-nav__link"> <span class="md-ellipsis"> Format Examples </span> </a></li> </ul> </nav></li> </ul> </nav> </div> </div> </div> <div class="md-content" data-md-component="content"> <article class="md-content__inner md-typeset"> <h1 id="format-string-syntax">Format String Syntax</h1> <p>Formatting functions such as <a href="https://fmt-dev.translate.goog/latest/api/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format"><code>fmt::format</code></a> and <a href="https://fmt-dev.translate.goog/latest/api/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#print"><code>fmt::print</code></a> use the same format string syntax described in this section.</p> <p>Format strings contain "replacement fields" surrounded by curly braces <code>{}</code>. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: <code>{{</code> and <code>}}</code>.</p> <p>The grammar for a replacement field is as follows:</p> <p><a id="replacement-field"></a></p> <pre><code class="language-json">replacement_field ::= "{" [arg_id] [":" (<a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-spec">format_spec</a> | <a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#chrono-format-spec">chrono_format_spec</a>)] "}" arg_id ::= integer | identifier integer ::= digit+ digit ::= "0"..."9" identifier ::= id_start id_continue* id_start ::= "a"..."z" | "A"..."Z" | "_" id_continue ::= id_start | digit</code> </pre> <p>In less formal terms, the replacement field can start with an <em>arg_id</em> that specifies the argument whose value is to be formatted and inserted into the output instead of the replacement field. The <em>arg_id</em> is optionally followed by a <em>format_spec</em>, which is preceded by a colon <code>':'</code>. These specify a non-default format for the replacement value.</p> <p>See also the <a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-specification-mini-language">Format Specification Mini-Language</a> section.</p> <p>If the numerical arg_ids in a format string are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order.</p> <p>Named arguments can be referred to by their names or indices.</p> <p>Some simple format string examples:</p> <pre><code class="language-c++">"First, thou shalt count to {0}" // References the first argument "Bring me a {}" // Implicitly references the first argument "From {} to {}" // Same as "From {0} to {1}" </code></pre> <p>The <em>format_spec</em> field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal precision and so on. Each value type can define its own "formatting mini-language" or interpretation of the <em>format_spec</em>.</p> <p>Most built-in types support a common formatting mini-language, which is described in the next section.</p> <p>A <em>format_spec</em> field can also include nested replacement fields in certain positions within it. These nested replacement fields can contain only an argument id; format specifications are not allowed. This allows the formatting of a value to be dynamically specified.</p> <p>See the <a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-examples">Format Examples</a> section for some examples.</p> <h2 id="format-specification-mini-language">Format Specification Mini-Language</h2> <p>"Format specifications" are used within replacement fields contained within a format string to define how individual values are presented. Each formattable type may define how the format specification is to be interpreted.</p> <p>Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types.</p> <p>The general form of a <em>standard format specifier</em> is:</p> <p><a id="format-spec"></a></p> <pre><code class="language-json">format_spec ::= [[fill]align][sign]["#"]["0"][width]["." precision]["L"][type] fill ::= <a character other than '{' or '}'> align ::= "<" | ">" | "^" sign ::= "+" | "-" | " " width ::= <a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#replacement-field">integer</a> | "{" [<a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#replacement-field">arg_id</a>] "}" precision ::= <a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#replacement-field">integer</a> | "{" [<a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#replacement-field">arg_id</a>] "}" type ::= "a" | "A" | "b" | "B" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "o" | "p" | "s" | "x" | "X" | "?"</code> </pre> <p>The <em>fill</em> character can be any Unicode code point other than <code>'{'</code> or <code>'}'</code>. The presence of a fill character is signaled by the character following it, which must be one of the alignment options. If the second character of <em>format_spec</em> is not a valid alignment option, then it is assumed that both the fill character and the alignment option are absent.</p> <p>The meaning of the various alignment options is as follows:</p> <table> <tbody> <tr> <th>Option</th> <th>Meaning</th> </tr> <tr> <td><code>'<'</code></td> <td>Forces the field to be left-aligned within the available space (this is the default for most objects).</td> </tr> <tr> <td><code>'>'</code></td> <td>Forces the field to be right-aligned within the available space (this is the default for numbers).</td> </tr> <tr> <td><code>'^'</code></td> <td>Forces the field to be centered within the available space.</td> </tr> </tbody> </table> <p>Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case.</p> <p>The <em>sign</em> option is only valid for floating point and signed integer types, and can be one of the following:</p> <table> <tbody> <tr> <th>Option</th> <th>Meaning</th> </tr> <tr> <td><code>'+'</code></td> <td>Indicates that a sign should be used for both nonnegative as well as negative numbers.</td> </tr> <tr> <td><code>'-'</code></td> <td>Indicates that a sign should be used only for negative numbers (this is the default behavior).</td> </tr> <tr> <td>space</td> <td>Indicates that a leading space should be used on nonnegative numbers, and a minus sign on negative numbers.</td> </tr> </tbody> </table> <p>The <code>'#'</code> option causes the "alternate form" to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer and floating-point types. For integers, when binary, octal, or hexadecimal output is used, this option adds the prefix respective <code>"0b"</code> (<code>"0B"</code>), <code>"0"</code>, or <code>"0x"</code> (<code>"0X"</code>) to the output value. Whether the prefix is lower-case or upper-case is determined by the case of the type specifier, for example, the prefix <code>"0x"</code> is used for the type <code>'x'</code> and <code>"0X"</code> is used for <code>'X'</code>. For floating-point numbers the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for <code>'g'</code> and <code>'G'</code> conversions, trailing zeros are not removed from the result.</p> <p><em>width</em> is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.</p> <p>Preceding the <em>width</em> field by a zero (<code>'0'</code>) character enables sign-aware zero-padding for numeric types. It forces the padding to be placed after the sign or base (if any) but before the digits. This is used for printing fields in the form "+000000120". This option is only valid for numeric types and it has no effect on formatting of infinity and NaN. This option is ignored when any alignment specifier is present.</p> <p>The <em>precision</em> is a decimal number indicating how many digits should be displayed after the decimal point for a floating-point value formatted with <code>'f'</code> and <code>'F'</code>, or before and after the decimal point for a floating-point value formatted with <code>'g'</code> or <code>'G'</code>. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The <em>precision</em> is not allowed for integer, character, Boolean, and pointer values. Note that a C string must be null-terminated even if precision is specified.</p> <p>The <code>'L'</code> option uses the current locale setting to insert the appropriate number separator characters. This option is only valid for numeric types.</p> <p>Finally, the <em>type</em> determines how the data should be presented.</p> <p>The available string presentation types are:</p> <table> <tbody> <tr> <th>Type</th> <th>Meaning</th> </tr> <tr> <td><code>'s'</code></td> <td>String format. This is the default type for strings and may be omitted.</td> </tr> <tr> <td><code>'?'</code></td> <td>Debug format. The string is quoted and special characters escaped.</td> </tr> <tr> <td>none</td> <td>The same as <code>'s'</code>.</td> </tr> </tbody> </table> <p>The available character presentation types are:</p> <table> <tbody> <tr> <th>Type</th> <th>Meaning</th> </tr> <tr> <td><code>'c'</code></td> <td>Character format. This is the default type for characters and may be omitted.</td> </tr> <tr> <td><code>'?'</code></td> <td>Debug format. The character is quoted and special characters escaped.</td> </tr> <tr> <td>none</td> <td>The same as <code>'c'</code>.</td> </tr> </tbody> </table> <p>The available integer presentation types are:</p> <table> <tbody> <tr> <th>Type</th> <th>Meaning</th> </tr> <tr> <td><code>'b'</code></td> <td>Binary format. Outputs the number in base 2. Using the <code>'#'</code> option with this type adds the prefix <code>"0b"</code> to the output value.</td> </tr> <tr> <td><code>'B'</code></td> <td>Binary format. Outputs the number in base 2. Using the <code>'#'</code> option with this type adds the prefix <code>"0B"</code> to the output value.</td> </tr> <tr> <td><code>'c'</code></td> <td>Character format. Outputs the number as a character.</td> </tr> <tr> <td><code>'d'</code></td> <td>Decimal integer. Outputs the number in base 10.</td> </tr> <tr> <td><code>'o'</code></td> <td>Octal format. Outputs the number in base 8.</td> </tr> <tr> <td><code>'x'</code></td> <td>Hex format. Outputs the number in base 16, using lower-case letters for the digits above 9. Using the <code>'#'</code> option with this type adds the prefix <code>"0x"</code> to the output value.</td> </tr> <tr> <td><code>'X'</code></td> <td>Hex format. Outputs the number in base 16, using upper-case letters for the digits above 9. Using the <code>'#'</code> option with this type adds the prefix <code>"0X"</code> to the output value.</td> </tr> <tr> <td>none</td> <td>The same as <code>'d'</code>.</td> </tr> </tbody> </table> <p>Integer presentation types can also be used with character and Boolean values with the only exception that <code>'c'</code> cannot be used with <code>bool</code>. Boolean values are formatted using textual representation, either <code>true</code> or <code>false</code>, if the presentation type is not specified.</p> <p>The available presentation types for floating-point values are:</p> <table> <tbody> <tr> <th>Type</th> <th>Meaning</th> </tr> <tr> <td><code>'a'</code></td> <td>Hexadecimal floating point format. Prints the number in base 16 with prefix <code>"0x"</code> and lower-case letters for digits above 9. Uses <code>'p'</code> to indicate the exponent.</td> </tr> <tr> <td><code>'A'</code></td> <td>Same as <code>'a'</code> except it uses upper-case letters for the prefix, digits above 9 and to indicate the exponent.</td> </tr> <tr> <td><code>'e'</code></td> <td>Exponent notation. Prints the number in scientific notation using the letter 'e' to indicate the exponent.</td> </tr> <tr> <td><code>'E'</code></td> <td>Exponent notation. Same as <code>'e'</code> except it uses an upper-case <code>'E'</code> as the separator character.</td> </tr> <tr> <td><code>'f'</code></td> <td>Fixed point. Displays the number as a fixed-point number.</td> </tr> <tr> <td><code>'F'</code></td> <td>Fixed point. Same as <code>'f'</code>, but converts <code>nan</code> to <code>NAN</code> and <code>inf</code> to <code>INF</code>.</td> </tr> <tr> <td><code>'g'</code></td> <td><p>General format. For a given precision <code>p >= 1</code>, this rounds the number to <code>p</code> significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.</p><p>A precision of <code>0</code> is treated as equivalent to a precision of <code>1</code>.</p></td> </tr> <tr> <td><code>'G'</code></td> <td>General format. Same as <code>'g'</code> except switches to <code>'E'</code> if the number gets too large. The representations of infinity and NaN are uppercased, too.</td> </tr> <tr> <td>none</td> <td>Similar to <code>'g'</code>, except that the default precision is as high as needed to represent the particular value.</td> </tr> </tbody> </table> <p>The available presentation types for pointers are:</p> <table> <tbody> <tr> <th>Type</th> <th>Meaning</th> </tr> <tr> <td><code>'p'</code></td> <td>Pointer format. This is the default type for pointers and may be omitted.</td> </tr> <tr> <td>none</td> <td>The same as <code>'p'</code>.</td> </tr> </tbody> </table> <h2 id="chrono-format-specifications">Chrono Format Specifications</h2> <p>Format specifications for chrono duration and time point types as well as <code>std::tm</code> have the following syntax:</p> <p><a id="chrono-format-spec"></a></p> <pre><code class="language-json">chrono_format_spec ::= [[<a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-spec">fill</a>]<a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-spec">align</a>][<a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-spec">width</a>]["." <a href="https://fmt-dev.translate.goog/latest/syntax/?_x_tr_sl=pl&_x_tr_tl=iw&_x_tr_hl=en-GB#format-spec">precision</a>][chrono_specs] chrono_specs ::= conversion_spec | chrono_specs (conversion_spec | literal_char) conversion_spec ::= "%" [padding_modifier] [locale_modifier] chrono_type literal_char ::= <a character other than '{', '}' or '%'> padding_modifier ::= "-" | "_" | "0" locale_modifier ::= "E" | "O" chrono_type ::= "a" | "A" | "b" | "B" | "c" | "C" | "d" | "D" | "e" | "F" | "g" | "G" | "h" | "H" | "I" | "j" | "m" | "M" | "n" | "p" | "q" | "Q" | "r" | "R" | "S" | "t" | "T" | "u" | "U" | "V" | "w" | "W" | "x" | "X" | "y" | "Y" | "z" | "Z" | "%"</code> </pre> <p>Literal chars are copied unchanged to the output. Precision is valid only for <code>std::chrono::duration</code> types with a floating-point representation type.</p> <p>The available presentation types (<em>chrono_type</em>) are:</p> <table> <tbody> <tr> <th>Type</th> <th>Meaning</th> </tr> <tr> <td><code>'a'</code></td> <td>The abbreviated weekday name, e.g. "Sat". If the value does not contain a valid weekday, an exception of type <code>format_error</code> is thrown.</td> </tr> <tr> <td><code>'A'</code></td> <td>The full weekday name, e.g. "Saturday". If the value does not contain a valid weekday, an exception of type <code>format_error</code> is thrown.</td> </tr> <tr> <td><code>'b'</code></td> <td>The abbreviated month name, e.g. "Nov". If the value does not contain a valid month, an exception of type <code>format_error</code> is thrown.</td> </tr> <tr> <td><code>'B'</code></td> <td>The full month name, e.g. "November". If the value does not contain a valid month, an exception of type <code>format_error</code> is thrown.</td> </tr> <tr> <td><code>'c'</code></td> <td>The date and time representation, e.g. "Sat Nov 12 22:04:00 1955". The modified command <code>%Ec</code> produces the locale's alternate date and time representation.</td> </tr> <tr> <td><code>'C'</code></td> <td>The year divided by 100 using floored division, e.g. "19". If the result is a single decimal digit, it is prefixed with 0. The modified command <code>%EC</code> produces the locale's alternative representation of the century.</td> </tr> <tr> <td><code>'d'</code></td> <td>The day of month as a decimal number. If the result is a single decimal digit, it is prefixed with 0. The modified command <code>%Od</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'D'</code></td> <td>Equivalent to <code>%m/%d/%y</code>, e.g. "11/12/55".</td> </tr> <tr> <td><code>'e'</code></td> <td>The day of month as a decimal number. If the result is a single decimal digit, it is prefixed with a space. The modified command <code>%Oe</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'F'</code></td> <td>Equivalent to <code>%Y-%m-%d</code>, e.g. "1955-11-12".</td> </tr> <tr> <td><code>'g'</code></td> <td>The last two decimal digits of the ISO week-based year. If the result is a single digit it is prefixed by 0.</td> </tr> <tr> <td><code>'G'</code></td> <td>The ISO week-based year as a decimal number. If the result is less than four digits it is left-padded with 0 to four digits.</td> </tr> <tr> <td><code>'h'</code></td> <td>Equivalent to <code>%b</code>, e.g. "Nov".</td> </tr> <tr> <td><code>'H'</code></td> <td>The hour (24-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command <code>%OH</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'I'</code></td> <td>The hour (12-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command <code>%OI</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'j'</code></td> <td>If the type being formatted is a specialization of duration, the decimal number of days without padding. Otherwise, the day of the year as a decimal number. Jan 1 is 001. If the result is less than three digits, it is left-padded with 0 to three digits.</td> </tr> <tr> <td><code>'m'</code></td> <td>The month as a decimal number. Jan is 01. If the result is a single digit, it is prefixed with 0. The modified command <code>%Om</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'M'</code></td> <td>The minute as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command <code>%OM</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'n'</code></td> <td>A new-line character.</td> </tr> <tr> <td><code>'p'</code></td> <td>The AM/PM designations associated with a 12-hour clock.</td> </tr> <tr> <td><code>'q'</code></td> <td>The duration's unit suffix.</td> </tr> <tr> <td><code>'Q'</code></td> <td>The duration's numeric value (as if extracted via <code>.count()</code>).</td> </tr> <tr> <td><code>'r'</code></td> <td>The 12-hour clock time, e.g. "10:04:00 PM".</td> </tr> <tr> <td><code>'R'</code></td> <td>Equivalent to <code>%H:%M</code>, e.g. "22:04".</td> </tr> <tr> <td><code>'S'</code></td> <td>Seconds as a decimal number. If the number of seconds is less than 10, the result is prefixed with 0. If the precision of the input cannot be exactly represented with seconds, then the format is a decimal floating-point number with a fixed format and a precision matching that of the precision of the input (or to a microseconds precision if the conversion to floating-point decimal seconds cannot be made within 18 fractional digits). The modified command <code>%OS</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'t'</code></td> <td>A horizontal-tab character.</td> </tr> <tr> <td><code>'T'</code></td> <td>Equivalent to <code>%H:%M:%S</code>.</td> </tr> <tr> <td><code>'u'</code></td> <td>The ISO weekday as a decimal number (1-7), where Monday is 1. The modified command <code>%Ou</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'U'</code></td> <td>The week number of the year as a decimal number. The first Sunday of the year is the first day of week 01. Days of the same year prior to that are in week 00. If the result is a single digit, it is prefixed with 0. The modified command <code>%OU</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'V'</code></td> <td>The ISO week-based week number as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command <code>%OV</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'w'</code></td> <td>The weekday as a decimal number (0-6), where Sunday is 0. The modified command <code>%Ow</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'W'</code></td> <td>The week number of the year as a decimal number. The first Monday of the year is the first day of week 01. Days of the same year prior to that are in week 00. If the result is a single digit, it is prefixed with 0. The modified command <code>%OW</code> produces the locale's alternative representation.</td> </tr> <tr> <td><code>'x'</code></td> <td>The date representation, e.g. "11/12/55". The modified command <code>%Ex</code> produces the locale's alternate date representation.</td> </tr> <tr> <td><code>'X'</code></td> <td>The time representation, e.g. "10:04:00". The modified command <code>%EX</code> produces the locale's alternate time representation.</td> </tr> <tr> <td><code>'y'</code></td> <td>The last two decimal digits of the year. If the result is a single digit it is prefixed by 0. The modified command <code>%Oy</code> produces the locale's alternative representation. The modified command <code>%Ey</code> produces the locale's alternative representation of offset from <code>%EC</code> (year only).</td> </tr> <tr> <td><code>'Y'</code></td> <td>The year as a decimal number. If the result is less than four digits it is left-padded with 0 to four digits. The modified command <code>%EY</code> produces the locale's alternative full year representation.</td> </tr> <tr> <td><code>'z'</code></td> <td>The offset from UTC in the ISO 8601:2004 format. For example -0430 refers to 4 hours 30 minutes behind UTC. If the offset is zero, +0000 is used. The modified commands <code>%Ez</code> and <code>%Oz</code> insert a <code>:</code> between the hours and minutes: -04:30. If the offset information is not available, an exception of type <code>format_error</code> is thrown.</td> </tr> <tr> <td><code>'Z'</code></td> <td>The time zone abbreviation. If the time zone abbreviation is not available, an exception of type <code>format_error</code> is thrown.</td> </tr> <tr> <td><code>'%'</code></td> <td>A % character.</td> </tr> </tbody> </table> <p>Specifiers that have a calendaric component such as <code>'d'</code> (the day of month) are valid only for <code>std::tm</code> and time points but not durations.</p> <p>The available padding modifiers (<em>padding_modifier</em>) are:</p> <table> <thead> <tr> <th>Type</th> <th>Meaning</th> </tr> </thead> <tbody> <tr> <td><code>'_'</code></td> <td>Pad a numeric result with spaces.</td> </tr> <tr> <td><code>'-'</code></td> <td>Do not pad a numeric result string.</td> </tr> <tr> <td><code>'0'</code></td> <td>Pad a numeric result string with zeros.</td> </tr> </tbody> </table> <p>These modifiers are only supported for the <code>'H'</code>, <code>'I'</code>, <code>'M'</code>, <code>'S'</code>, <code>'U'</code>, <code>'V'</code>, <code>'W'</code>, <code>'Y'</code>, <code>'d'</code>, <code>'j'</code> and <code>'m'</code> presentation types.</p> <h2 id="range-format-specifications">Range Format Specifications</h2> <p>Format specifications for range types have the following syntax:</p> <pre><code class="language-json">range_format_spec ::= ["n"][range_type][range_underlying_spec]</code> </pre> <p>The <code>'n'</code> option formats the range without the opening and closing brackets.</p> <p>The available presentation types for <code>range_type</code> are:</p> <table> <thead> <tr> <th>Type</th> <th>Meaning</th> </tr> </thead> <tbody> <tr> <td>none</td> <td>Default format.</td> </tr> <tr> <td><code>'s'</code></td> <td>String format. The range is formatted as a string.</td> </tr> <tr> <td><code>'?s'</code></td> <td>Debug format. The range is formatted as an escaped string.</td> </tr> </tbody> </table> <p>If <code>range_type</code> is <code>'s'</code> or <code>'?s'</code>, the range element type must be a character type. The <code>'n'</code> option and <code>range_underlying_spec</code> are mutually exclusive with <code>'s'</code> and <code>'?s'</code>.</p> <p>The <code>range_underlying_spec</code> is parsed based on the formatter of the range's element type.</p> <p>By default, a range of characters or strings is printed escaped and quoted. But if any <code>range_underlying_spec</code> is provided (even if it is empty), then the characters or strings are printed according to the provided specification.</p> <p>Examples:</p> <pre><code class="language-c++">fmt::print("{}", std::vector{10, 20, 30}); // Output: [10, 20, 30] fmt::print("{::#x}", std::vector{10, 20, 30}); // Output: [0xa, 0x14, 0x1e] fmt::print("{}", std::vector{'h', 'e', 'l', 'l', 'o'}); // Output: ['h', 'e', 'l', 'l', 'o'] fmt::print("{:n}", std::vector{'h', 'e', 'l', 'l', 'o'}); // Output: 'h', 'e', 'l', 'l', 'o' fmt::print("{:s}", std::vector{'h', 'e', 'l', 'l', 'o'}); // Output: "hello" fmt::print("{:?s}", std::vector{'h', 'e', 'l', 'l', 'o', '\n'}); // Output: "hello\n" fmt::print("{::}", std::vector{'h', 'e', 'l', 'l', 'o'}); // Output: [h, e, l, l, o] fmt::print("{::d}", std::vector{'h', 'e', 'l', 'l', 'o'}); // Output: [104, 101, 108, 108, 111] </code></pre> <h2 id="format-examples">Format Examples</h2> <p>This section contains examples of the format syntax and comparison with the printf formatting.</p> <p>In most of the cases the syntax is similar to the printf formatting, with the addition of the <code>{}</code> and with <code>:</code> used instead of <code>%</code>. For example, <code>"%03.2f"</code> can be translated to <code>"{:03.2f}"</code>.</p> <p>The new format syntax also supports new and different options, shown in the following examples.</p> <p>Accessing arguments by position:</p> <pre><code class="language-c++">fmt::format("{0}, {1}, {2}", 'a', 'b', 'c'); // Result: "a, b, c" fmt::format("{}, {}, {}", 'a', 'b', 'c'); // Result: "a, b, c" fmt::format("{2}, {1}, {0}", 'a', 'b', 'c'); // Result: "c, b, a" fmt::format("{0}{1}{0}", "abra", "cad"); // arguments' indices can be repeated // Result: "abracadabra" </code></pre> <p>Aligning the text and specifying a width:</p> <pre><code class="language-c++">fmt::format("{:<30}", "left aligned"); // Result: "left aligned " fmt::format("{:>30}", "right aligned"); // Result: " right aligned" fmt::format("{:^30}", "centered"); // Result: " centered " fmt::format("{:*^30}", "centered"); // use '*' as a fill char // Result: "***********centered***********" </code></pre> <p>Dynamic width:</p> <pre><code class="language-c++">fmt::format("{:<{}}", "left aligned", 30); // Result: "left aligned " </code></pre> <p>Dynamic precision:</p> <pre><code class="language-c++">fmt::format("{:.{}f}", 3.14, 1); // Result: "3.1" </code></pre> <p>Replacing <code>%+f</code>, <code>%-f</code>, and <code>% f</code> and specifying a sign:</p> <pre><code class="language-c++">fmt::format("{:+f}; {:+f}", 3.14, -3.14); // show it always // Result: "+3.140000; -3.140000" fmt::format("{: f}; {: f}", 3.14, -3.14); // show a space for positive numbers // Result: " 3.140000; -3.140000" fmt::format("{:-f}; {:-f}", 3.14, -3.14); // show only the minus -- same as '{:f}; {:f}' // Result: "3.140000; -3.140000" </code></pre> <p>Replacing <code>%x</code> and <code>%o</code> and converting the value to different bases:</p> <pre><code class="language-c++">fmt::format("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42); // Result: "int: 42; hex: 2a; oct: 52; bin: 101010" // with 0x or 0 or 0b as prefix: fmt::format("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}", 42); // Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010" </code></pre> <p>Padded hex byte with prefix and always prints both hex characters:</p> <pre><code class="language-c++">fmt::format("{:#04x}", 0); // Result: "0x00" </code></pre> <p>Box drawing using Unicode fill:</p> <pre><code class="language-c++">fmt::print( "┌{0:─^{2}}┐\n" "│{1: ^{2}}│\n" "└{0:─^{2}}┘\n", "", "Hello, world!", 20); </code></pre> <p>prints:</p> <pre><code>┌────────────────────┐ │ Hello, world! │ └────────────────────┘ </code></pre> <p>Using type-specific formatting:</p> <pre><code class="language-c++">#include <fmt/chrono.h> auto t = tm(); t.tm_year = 2010 - 1900; t.tm_mon = 7; t.tm_mday = 4; t.tm_hour = 12; t.tm_min = 15; t.tm_sec = 58; fmt::print("{:%Y-%m-%d %H:%M:%S}", t); // Prints: 2010-08-04 12:15:58 </code></pre> <p>Using the comma as a thousands separator:</p> <pre><code class="language-c++">#include <fmt/format.h> auto s = fmt::format(std::locale("en_US.UTF-8"), "{:L}", 1234567890); // s == "1,234,567,890" </code></pre> </article> </div> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div><button type="button" class="md-top md-icon" data-md-component="top" hidden> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24"> <path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12Z" /> </svg> Back to top </button> </main> <footer class="md-footer"> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class="md-copyright"> </div> </div> </div> </footer> </div> <div class="md-dialog" data-md-component="dialog"> <div class="md-dialog__inner md-typeset"></div> </div> <script id="__config" type="application/json">{"base": "..", "features": ["navigation.tabs", "navigation.top", "toc.integrate"], "search": "../assets/javascripts/workers/search.b8dbb3d2.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}, "version": {"provider": "mike"}}</script> <script src="../assets/javascripts/bundle.081f42fc.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/highlight.min.js"></script> <script src="../fmt.js"></script> <script>function gtElInit() {var lib = new google.translate.TranslateService();lib.translatePage('pl', 'iw', function () {});}</script> <script src="https://translate.google.com/translate_a/element.js?cb=gtElInit&hl=en-GB&client=wt" type="text/javascript"></script> </body> </html>