syntax.html 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  8. <title>Format String Syntax &mdash; fmt 9.1.0 documentation</title>
  9. <link rel="stylesheet" href="_static/basic.css" type="text/css" />
  10. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  11. <link rel="stylesheet" href="_static/breathe.css" type="text/css" />
  12. <script type="text/javascript">
  13. var DOCUMENTATION_OPTIONS = {
  14. URL_ROOT: './',
  15. VERSION: '9.1.0',
  16. COLLAPSE_INDEX: false,
  17. FILE_SUFFIX: '.html',
  18. LINK_SUFFIX: '.html',
  19. SOURCELINK_SUFFIX: '.txt',
  20. HAS_SOURCE: true,
  21. SOURCELINK_SUFFIX: '.txt'
  22. };
  23. </script>
  24. <script src="_static/jquery.js"></script>
  25. <script src="_static/underscore.js"></script>
  26. <script src="_static/doctools.js"></script>
  27. <script src="_static/language_data.js"></script>
  28. <link rel="index" title="Index" href="genindex.html" />
  29. <link rel="search" title="Search" href="search.html" />
  30. <meta name="description" content="Small, safe and fast formatting library">
  31. <meta name="keywords" content="C++, formatting, printf, string, library">
  32. <meta name="author" content="Victor Zverovich">
  33. <link rel="stylesheet" href="_static/fmt.css">
  34. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-20116650-4"></script>
  35. <script>
  36. window.dataLayer = window.dataLayer || [];
  37. function gtag(){dataLayer.push(arguments);}
  38. gtag('js', new Date());
  39. gtag('config', 'UA-20116650-4');
  40. </script>
  41. </head>
  42. <body role="document">
  43. <nav class="navbar navbar-inverse">
  44. <div class="tb-container">
  45. <div class="row">
  46. <div class="navbar-content">
  47. <div class="navbar-header">
  48. <button type="button" class="navbar-toggle collapsed"
  49. data-toggle="collapse" data-target=".navbar-collapse">
  50. <span class="sr-only">Toggle navigation</span>
  51. <span class="icon-bar"></span>
  52. <span class="icon-bar"></span>
  53. <span class="icon-bar"></span>
  54. </button>
  55. <a class="navbar-brand" href="index.html">{fmt}</a>
  56. </div>
  57. <div class="collapse navbar-collapse">
  58. <ul class="nav navbar-nav">
  59. <li class="dropdown">
  60. <a href="#" class="dropdown-toggle" data-toggle="dropdown"
  61. role="button" aria-expanded="false">9.1.0
  62. <span class="caret"></span></a>
  63. <ul class="dropdown-menu" role="menu">
  64. <li><a href="https://fmt.dev/9.1.0">9.1.0</a></li>
  65. <li><a href="https://fmt.dev/9.0.0">9.0.0</a></li>
  66. <li><a href="https://fmt.dev/8.1.1">8.1.1</a></li>
  67. </ul>
  68. </li>
  69. <li><a href="contents.html">Contents</a></li>
  70. <li><a href="usage.html">Usage</a></li>
  71. <li><a href="api.html">API</a></li>
  72. <li class="active"><a href="syntax.html">Syntax
  73. <span class="sr-only">(current)</span></a></li>
  74. </ul>
  75. <form class="navbar-form navbar-right" role="search" action="search.html"
  76. method="get">
  77. <div class="form-group">
  78. <input type="text" name="q" class="form-control"
  79. placeholder="Search" >
  80. </div>
  81. <input type="hidden" name="check_keywords" value="yes" />
  82. <input type="hidden" name="area" value="default" />
  83. </form>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. </nav>
  89. <div class="tb-container">
  90. <div class="row">
  91. <div class="content">
  92. <section id="format-string-syntax">
  93. <span id="syntax"></span><h1>Format String Syntax<a class="headerlink" href="#format-string-syntax" title="Permalink to this headline">¶</a></h1>
  94. <p>Formatting functions such as <a class="reference internal" href="api.html#format"><span class="std std-ref">fmt::format()</span></a> and
  95. <a class="reference internal" href="api.html#print"><span class="std std-ref">fmt::print()</span></a> use the same format string syntax described in this
  96. section.</p>
  97. <p>Format strings contain “replacement fields” surrounded by curly braces <code class="docutils literal notranslate"><span class="pre">{}</span></code>.
  98. Anything that is not contained in braces is considered literal text, which is
  99. copied unchanged to the output. If you need to include a brace character in the
  100. literal text, it can be escaped by doubling: <code class="docutils literal notranslate"><span class="pre">{{</span></code> and <code class="docutils literal notranslate"><span class="pre">}}</span></code>.</p>
  101. <p>The grammar for a replacement field is as follows:</p>
  102. <pre>
  103. <strong id="grammar-token-sf-replacement_field"><span id="grammar-token-replacement-field"></span>replacement_field</strong> ::= &quot;{&quot; [<a class="reference internal" href="#grammar-token-sf-arg_id"><code class="xref docutils literal notranslate"><span class="pre">arg_id</span></code></a>] [&quot;:&quot; (<a class="reference internal" href="#grammar-token-sf-format_spec"><code class="xref docutils literal notranslate"><span class="pre">format_spec</span></code></a> | <a class="reference internal" href="#grammar-token-sf-chrono_format_spec"><code class="xref docutils literal notranslate"><span class="pre">chrono_format_spec</span></code></a>)] &quot;}&quot;
  104. <strong id="grammar-token-sf-arg_id"><span id="grammar-token-arg-id"></span>arg_id </strong> ::= <a class="reference internal" href="#grammar-token-sf-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | <a class="reference internal" href="#grammar-token-sf-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>
  105. <strong id="grammar-token-sf-integer"><span id="grammar-token-integer"></span>integer </strong> ::= <a class="reference internal" href="#grammar-token-sf-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>+
  106. <strong id="grammar-token-sf-digit"><span id="grammar-token-digit"></span>digit </strong> ::= &quot;0&quot;...&quot;9&quot;
  107. <strong id="grammar-token-sf-identifier"><span id="grammar-token-identifier"></span>identifier </strong> ::= <a class="reference internal" href="#grammar-token-sf-id_start"><code class="xref docutils literal notranslate"><span class="pre">id_start</span></code></a> <a class="reference internal" href="#grammar-token-sf-id_continue"><code class="xref docutils literal notranslate"><span class="pre">id_continue</span></code></a>*
  108. <strong id="grammar-token-sf-id_start"><span id="grammar-token-id-start"></span>id_start </strong> ::= &quot;a&quot;...&quot;z&quot; | &quot;A&quot;...&quot;Z&quot; | &quot;_&quot;
  109. <strong id="grammar-token-sf-id_continue"><span id="grammar-token-id-continue"></span>id_continue </strong> ::= <a class="reference internal" href="#grammar-token-sf-id_start"><code class="xref docutils literal notranslate"><span class="pre">id_start</span></code></a> | <a class="reference internal" href="#grammar-token-sf-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>
  110. </pre>
  111. <p>In less formal terms, the replacement field can start with an <em>arg_id</em>
  112. that specifies the argument whose value is to be formatted and inserted into
  113. the output instead of the replacement field.
  114. The <em>arg_id</em> is optionally followed by a <em>format_spec</em>, which is preceded by a
  115. colon <code class="docutils literal notranslate"><span class="pre">':'</span></code>. These specify a non-default format for the replacement value.</p>
  116. <p>See also the <a class="reference internal" href="#formatspec"><span class="std std-ref">Format Specification Mini-Language</span></a> section.</p>
  117. <p>If the numerical arg_ids in a format string are 0, 1, 2, … in sequence,
  118. they can all be omitted (not just some) and the numbers 0, 1, 2, … will be
  119. automatically inserted in that order.</p>
  120. <p>Named arguments can be referred to by their names or indices.</p>
  121. <p>Some simple format string examples:</p>
  122. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="s">&quot;First, thou shalt count to {0}&quot;</span><span class="w"> </span><span class="c1">// References the first argument</span>
  123. <span class="s">&quot;Bring me a {}&quot;</span><span class="w"> </span><span class="c1">// Implicitly references the first argument</span>
  124. <span class="s">&quot;From {} to {}&quot;</span><span class="w"> </span><span class="c1">// Same as &quot;From {0} to {1}&quot;</span>
  125. </pre></div>
  126. </div>
  127. <p>The <em>format_spec</em> field contains a specification of how the value should be
  128. presented, including such details as field width, alignment, padding, decimal
  129. precision and so on. Each value type can define its own “formatting
  130. mini-language” or interpretation of the <em>format_spec</em>.</p>
  131. <p>Most built-in types support a common formatting mini-language, which is
  132. described in the next section.</p>
  133. <p>A <em>format_spec</em> field can also include nested replacement fields in certain
  134. positions within it. These nested replacement fields can contain only an
  135. argument id; format specifications are not allowed. This allows the formatting
  136. of a value to be dynamically specified.</p>
  137. <p>See the <a class="reference internal" href="#formatexamples"><span class="std std-ref">Format Examples</span></a> section for some examples.</p>
  138. <section id="format-specification-mini-language">
  139. <span id="formatspec"></span><h2>Format Specification Mini-Language<a class="headerlink" href="#format-specification-mini-language" title="Permalink to this headline">¶</a></h2>
  140. <p>“Format specifications” are used within replacement fields contained within a
  141. format string to define how individual values are presented (see
  142. <a class="reference internal" href="#syntax"><span class="std std-ref">Format String Syntax</span></a>). Each formattable type may define how the format
  143. specification is to be interpreted.</p>
  144. <p>Most built-in types implement the following options for format specifications,
  145. although some of the formatting options are only supported by the numeric types.</p>
  146. <p>The general form of a <em>standard format specifier</em> is:</p>
  147. <pre>
  148. <strong id="grammar-token-sf-format_spec"><span id="grammar-token-format-spec"></span>format_spec</strong> ::= [[<a class="reference internal" href="#grammar-token-sf-fill"><code class="xref docutils literal notranslate"><span class="pre">fill</span></code></a>]<a class="reference internal" href="#grammar-token-sf-align"><code class="xref docutils literal notranslate"><span class="pre">align</span></code></a>][<a class="reference internal" href="#grammar-token-sf-sign"><code class="xref docutils literal notranslate"><span class="pre">sign</span></code></a>][&quot;#&quot;][&quot;0&quot;][<a class="reference internal" href="#grammar-token-sf-width"><code class="xref docutils literal notranslate"><span class="pre">width</span></code></a>][&quot;.&quot; <a class="reference internal" href="#grammar-token-sf-precision"><code class="xref docutils literal notranslate"><span class="pre">precision</span></code></a>][&quot;L&quot;][<a class="reference internal" href="#grammar-token-sf-type"><code class="xref docutils literal notranslate"><span class="pre">type</span></code></a>]
  149. <strong id="grammar-token-sf-fill"><span id="grammar-token-fill"></span>fill </strong> ::= &lt;a character other than '{' or '}'&gt;
  150. <strong id="grammar-token-sf-align"><span id="grammar-token-align"></span>align </strong> ::= &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;^&quot;
  151. <strong id="grammar-token-sf-sign"><span id="grammar-token-sign"></span>sign </strong> ::= &quot;+&quot; | &quot;-&quot; | &quot; &quot;
  152. <strong id="grammar-token-sf-width"><span id="grammar-token-width"></span>width </strong> ::= <a class="reference internal" href="#grammar-token-sf-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | &quot;{&quot; [<a class="reference internal" href="#grammar-token-sf-arg_id"><code class="xref docutils literal notranslate"><span class="pre">arg_id</span></code></a>] &quot;}&quot;
  153. <strong id="grammar-token-sf-precision"><span id="grammar-token-precision"></span>precision </strong> ::= <a class="reference internal" href="#grammar-token-sf-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | &quot;{&quot; [<a class="reference internal" href="#grammar-token-sf-arg_id"><code class="xref docutils literal notranslate"><span class="pre">arg_id</span></code></a>] &quot;}&quot;
  154. <strong id="grammar-token-sf-type"><span id="grammar-token-type"></span>type </strong> ::= &quot;a&quot; | &quot;A&quot; | &quot;b&quot; | &quot;B&quot; | &quot;c&quot; | &quot;d&quot; | &quot;e&quot; | &quot;E&quot; | &quot;f&quot; | &quot;F&quot; | &quot;g&quot; | &quot;G&quot; |
  155. &quot;o&quot; | &quot;p&quot; | &quot;s&quot; | &quot;x&quot; | &quot;X&quot;
  156. </pre>
  157. <p>The <em>fill</em> character can be any Unicode code point other than <code class="docutils literal notranslate"><span class="pre">'{'</span></code> or
  158. <code class="docutils literal notranslate"><span class="pre">'}'</span></code>. The presence of a fill character is signaled by the character following
  159. it, which must be one of the alignment options. If the second character of
  160. <em>format_spec</em> is not a valid alignment option, then it is assumed that both the
  161. fill character and the alignment option are absent.</p>
  162. <p>The meaning of the various alignment options is as follows:</p>
  163. <table class="docutils align-default">
  164. <colgroup>
  165. <col style="width: 13%" />
  166. <col style="width: 87%" />
  167. </colgroup>
  168. <thead>
  169. <tr class="row-odd"><th class="head"><p>Option</p></th>
  170. <th class="head"><p>Meaning</p></th>
  171. </tr>
  172. </thead>
  173. <tbody>
  174. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'&lt;'</span></code></p></td>
  175. <td><p>Forces the field to be left-aligned within the available
  176. space (this is the default for most objects).</p></td>
  177. </tr>
  178. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'&gt;'</span></code></p></td>
  179. <td><p>Forces the field to be right-aligned within the
  180. available space (this is the default for numbers).</p></td>
  181. </tr>
  182. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'^'</span></code></p></td>
  183. <td><p>Forces the field to be centered within the available
  184. space.</p></td>
  185. </tr>
  186. </tbody>
  187. </table>
  188. <p>Note that unless a minimum field width is defined, the field width will always
  189. be the same size as the data to fill it, so that the alignment option has no
  190. meaning in this case.</p>
  191. <p>The <em>sign</em> option is only valid for number types, and can be one of the
  192. following:</p>
  193. <table class="docutils align-default">
  194. <colgroup>
  195. <col style="width: 13%" />
  196. <col style="width: 87%" />
  197. </colgroup>
  198. <thead>
  199. <tr class="row-odd"><th class="head"><p>Option</p></th>
  200. <th class="head"><p>Meaning</p></th>
  201. </tr>
  202. </thead>
  203. <tbody>
  204. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'+'</span></code></p></td>
  205. <td><p>indicates that a sign should be used for both
  206. nonnegative as well as negative numbers.</p></td>
  207. </tr>
  208. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'-'</span></code></p></td>
  209. <td><p>indicates that a sign should be used only for negative
  210. numbers (this is the default behavior).</p></td>
  211. </tr>
  212. <tr class="row-even"><td><p>space</p></td>
  213. <td><p>indicates that a leading space should be used on
  214. nonnegative numbers, and a minus sign on negative numbers.</p></td>
  215. </tr>
  216. </tbody>
  217. </table>
  218. <p>The <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option causes the “alternate form” to be used for the
  219. conversion. The alternate form is defined differently for different
  220. types. This option is only valid for integer and floating-point types.
  221. For integers, when binary, octal, or hexadecimal output is used, this
  222. option adds the prefix respective <code class="docutils literal notranslate"><span class="pre">&quot;0b&quot;</span></code> (<code class="docutils literal notranslate"><span class="pre">&quot;0B&quot;</span></code>), <code class="docutils literal notranslate"><span class="pre">&quot;0&quot;</span></code>, or
  223. <code class="docutils literal notranslate"><span class="pre">&quot;0x&quot;</span></code> (<code class="docutils literal notranslate"><span class="pre">&quot;0X&quot;</span></code>) to the output value. Whether the prefix is
  224. lower-case or upper-case is determined by the case of the type
  225. specifier, for example, the prefix <code class="docutils literal notranslate"><span class="pre">&quot;0x&quot;</span></code> is used for the type <code class="docutils literal notranslate"><span class="pre">'x'</span></code>
  226. and <code class="docutils literal notranslate"><span class="pre">&quot;0X&quot;</span></code> is used for <code class="docutils literal notranslate"><span class="pre">'X'</span></code>. For floating-point numbers the
  227. alternate form causes the result of the conversion to always contain a
  228. decimal-point character, even if no digits follow it. Normally, a
  229. decimal-point character appears in the result of these conversions
  230. only if a digit follows it. In addition, for <code class="docutils literal notranslate"><span class="pre">'g'</span></code> and <code class="docutils literal notranslate"><span class="pre">'G'</span></code>
  231. conversions, trailing zeros are not removed from the result.</p>
  232. <p><em>width</em> is a decimal integer defining the minimum field width. If not
  233. specified, then the field width will be determined by the content.</p>
  234. <p>Preceding the <em>width</em> field by a zero (<code class="docutils literal notranslate"><span class="pre">'0'</span></code>) character enables sign-aware
  235. zero-padding for numeric types. It forces the padding to be placed after the
  236. sign or base (if any) but before the digits. This is used for printing fields in
  237. the form ‘+000000120’. This option is only valid for numeric types and it has no
  238. effect on formatting of infinity and NaN.</p>
  239. <p>The <em>precision</em> is a decimal number indicating how many digits should be
  240. displayed after the decimal point for a floating-point value formatted with
  241. <code class="docutils literal notranslate"><span class="pre">'f'</span></code> and <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, or before and after the decimal point for a floating-point
  242. value formatted with <code class="docutils literal notranslate"><span class="pre">'g'</span></code> or <code class="docutils literal notranslate"><span class="pre">'G'</span></code>. For non-number types the field
  243. indicates the maximum field size - in other words, how many characters will be
  244. used from the field content. The <em>precision</em> is not allowed for integer,
  245. character, Boolean, and pointer values. Note that a C string must be
  246. null-terminated even if precision is specified.</p>
  247. <p>The <code class="docutils literal notranslate"><span class="pre">'L'</span></code> option uses the current locale setting to insert the appropriate
  248. number separator characters. This option is only valid for numeric types.</p>
  249. <p>Finally, the <em>type</em> determines how the data should be presented.</p>
  250. <p>The available string presentation types are:</p>
  251. <table class="docutils align-default">
  252. <colgroup>
  253. <col style="width: 13%" />
  254. <col style="width: 87%" />
  255. </colgroup>
  256. <thead>
  257. <tr class="row-odd"><th class="head"><p>Type</p></th>
  258. <th class="head"><p>Meaning</p></th>
  259. </tr>
  260. </thead>
  261. <tbody>
  262. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'s'</span></code></p></td>
  263. <td><p>String format. This is the default type for strings and
  264. may be omitted.</p></td>
  265. </tr>
  266. <tr class="row-odd"><td><p>none</p></td>
  267. <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'s'</span></code>.</p></td>
  268. </tr>
  269. </tbody>
  270. </table>
  271. <p>The available character presentation types are:</p>
  272. <table class="docutils align-default">
  273. <colgroup>
  274. <col style="width: 13%" />
  275. <col style="width: 87%" />
  276. </colgroup>
  277. <thead>
  278. <tr class="row-odd"><th class="head"><p>Type</p></th>
  279. <th class="head"><p>Meaning</p></th>
  280. </tr>
  281. </thead>
  282. <tbody>
  283. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code></p></td>
  284. <td><p>Character format. This is the default type for
  285. characters and may be omitted.</p></td>
  286. </tr>
  287. <tr class="row-odd"><td><p>none</p></td>
  288. <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'c'</span></code>.</p></td>
  289. </tr>
  290. </tbody>
  291. </table>
  292. <p>The available integer presentation types are:</p>
  293. <table class="docutils align-default">
  294. <colgroup>
  295. <col style="width: 13%" />
  296. <col style="width: 87%" />
  297. </colgroup>
  298. <thead>
  299. <tr class="row-odd"><th class="head"><p>Type</p></th>
  300. <th class="head"><p>Meaning</p></th>
  301. </tr>
  302. </thead>
  303. <tbody>
  304. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>
  305. <td><p>Binary format. Outputs the number in base 2. Using the
  306. <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option with this type adds the prefix <code class="docutils literal notranslate"><span class="pre">&quot;0b&quot;</span></code>
  307. to the output value.</p></td>
  308. </tr>
  309. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td>
  310. <td><p>Binary format. Outputs the number in base 2. Using the
  311. <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option with this type adds the prefix <code class="docutils literal notranslate"><span class="pre">&quot;0B&quot;</span></code>
  312. to the output value.</p></td>
  313. </tr>
  314. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code></p></td>
  315. <td><p>Character format. Outputs the number as a character.</p></td>
  316. </tr>
  317. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td>
  318. <td><p>Decimal integer. Outputs the number in base 10.</p></td>
  319. </tr>
  320. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'o'</span></code></p></td>
  321. <td><p>Octal format. Outputs the number in base 8.</p></td>
  322. </tr>
  323. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'x'</span></code></p></td>
  324. <td><p>Hex format. Outputs the number in base 16, using
  325. lower-case letters for the digits above 9. Using the
  326. <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option with this type adds the prefix <code class="docutils literal notranslate"><span class="pre">&quot;0x&quot;</span></code>
  327. to the output value.</p></td>
  328. </tr>
  329. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'X'</span></code></p></td>
  330. <td><p>Hex format. Outputs the number in base 16, using
  331. upper-case letters for the digits above 9. Using the
  332. <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option with this type adds the prefix <code class="docutils literal notranslate"><span class="pre">&quot;0X&quot;</span></code>
  333. to the output value.</p></td>
  334. </tr>
  335. <tr class="row-odd"><td><p>none</p></td>
  336. <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'d'</span></code>.</p></td>
  337. </tr>
  338. </tbody>
  339. </table>
  340. <p>Integer presentation types can also be used with character and Boolean values.
  341. Boolean values are formatted using textual representation, either <code class="docutils literal notranslate"><span class="pre">true</span></code> or
  342. <code class="docutils literal notranslate"><span class="pre">false</span></code>, if the presentation type is not specified.</p>
  343. <p>The available presentation types for floating-point values are:</p>
  344. <table class="docutils align-default">
  345. <colgroup>
  346. <col style="width: 13%" />
  347. <col style="width: 87%" />
  348. </colgroup>
  349. <thead>
  350. <tr class="row-odd"><th class="head"><p>Type</p></th>
  351. <th class="head"><p>Meaning</p></th>
  352. </tr>
  353. </thead>
  354. <tbody>
  355. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'a'</span></code></p></td>
  356. <td><p>Hexadecimal floating point format. Prints the number in
  357. base 16 with prefix <code class="docutils literal notranslate"><span class="pre">&quot;0x&quot;</span></code> and lower-case letters for
  358. digits above 9. Uses <code class="docutils literal notranslate"><span class="pre">'p'</span></code> to indicate the exponent.</p></td>
  359. </tr>
  360. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'A'</span></code></p></td>
  361. <td><p>Same as <code class="docutils literal notranslate"><span class="pre">'a'</span></code> except it uses upper-case letters for
  362. the prefix, digits above 9 and to indicate the exponent.</p></td>
  363. </tr>
  364. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'e'</span></code></p></td>
  365. <td><p>Exponent notation. Prints the number in scientific
  366. notation using the letter ‘e’ to indicate the exponent.</p></td>
  367. </tr>
  368. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'E'</span></code></p></td>
  369. <td><p>Exponent notation. Same as <code class="docutils literal notranslate"><span class="pre">'e'</span></code> except it uses an
  370. upper-case <code class="docutils literal notranslate"><span class="pre">'E'</span></code> as the separator character.</p></td>
  371. </tr>
  372. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td>
  373. <td><p>Fixed point. Displays the number as a fixed-point
  374. number.</p></td>
  375. </tr>
  376. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'F'</span></code></p></td>
  377. <td><p>Fixed point. Same as <code class="docutils literal notranslate"><span class="pre">'f'</span></code>, but converts <code class="docutils literal notranslate"><span class="pre">nan</span></code> to
  378. <code class="docutils literal notranslate"><span class="pre">NAN</span></code> and <code class="docutils literal notranslate"><span class="pre">inf</span></code> to <code class="docutils literal notranslate"><span class="pre">INF</span></code>.</p></td>
  379. </tr>
  380. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'g'</span></code></p></td>
  381. <td><p>General format. For a given precision <code class="docutils literal notranslate"><span class="pre">p</span> <span class="pre">&gt;=</span> <span class="pre">1</span></code>,
  382. this rounds the number to <code class="docutils literal notranslate"><span class="pre">p</span></code> significant digits and
  383. then formats the result in either fixed-point format
  384. or in scientific notation, depending on its magnitude.</p>
  385. <p>A precision of <code class="docutils literal notranslate"><span class="pre">0</span></code> is treated as equivalent to a
  386. precision of <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  387. </td>
  388. </tr>
  389. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'G'</span></code></p></td>
  390. <td><p>General format. Same as <code class="docutils literal notranslate"><span class="pre">'g'</span></code> except switches to
  391. <code class="docutils literal notranslate"><span class="pre">'E'</span></code> if the number gets too large. The
  392. representations of infinity and NaN are uppercased, too.</p></td>
  393. </tr>
  394. <tr class="row-even"><td><p>none</p></td>
  395. <td><p>Similar to <code class="docutils literal notranslate"><span class="pre">'g'</span></code>, except that the default precision is
  396. as high as needed to represent the particular value.</p></td>
  397. </tr>
  398. </tbody>
  399. </table>
  400. <p>The available presentation types for pointers are:</p>
  401. <table class="docutils align-default">
  402. <colgroup>
  403. <col style="width: 13%" />
  404. <col style="width: 87%" />
  405. </colgroup>
  406. <thead>
  407. <tr class="row-odd"><th class="head"><p>Type</p></th>
  408. <th class="head"><p>Meaning</p></th>
  409. </tr>
  410. </thead>
  411. <tbody>
  412. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'p'</span></code></p></td>
  413. <td><p>Pointer format. This is the default type for
  414. pointers and may be omitted.</p></td>
  415. </tr>
  416. <tr class="row-odd"><td><p>none</p></td>
  417. <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'p'</span></code>.</p></td>
  418. </tr>
  419. </tbody>
  420. </table>
  421. </section>
  422. <section id="chrono-format-specifications">
  423. <span id="chrono-specs"></span><h2>Chrono Format Specifications<a class="headerlink" href="#chrono-format-specifications" title="Permalink to this headline">¶</a></h2>
  424. <p>Format specifications for chrono types and <code class="docutils literal notranslate"><span class="pre">std::tm</span></code> have the following
  425. syntax:</p>
  426. <pre>
  427. <strong id="grammar-token-sf-chrono_format_spec"><span id="grammar-token-chrono-format-spec"></span>chrono_format_spec</strong> ::= [[<a class="reference internal" href="#grammar-token-sf-fill"><code class="xref docutils literal notranslate"><span class="pre">fill</span></code></a>]<a class="reference internal" href="#grammar-token-sf-align"><code class="xref docutils literal notranslate"><span class="pre">align</span></code></a>][<a class="reference internal" href="#grammar-token-sf-width"><code class="xref docutils literal notranslate"><span class="pre">width</span></code></a>][&quot;.&quot; <a class="reference internal" href="#grammar-token-sf-precision"><code class="xref docutils literal notranslate"><span class="pre">precision</span></code></a>][<a class="reference internal" href="#grammar-token-sf-chrono_specs"><code class="xref docutils literal notranslate"><span class="pre">chrono_specs</span></code></a>]
  428. <strong id="grammar-token-sf-chrono_specs"><span id="grammar-token-chrono-specs"></span>chrono_specs </strong> ::= [<a class="reference internal" href="#grammar-token-sf-chrono_specs"><code class="xref docutils literal notranslate"><span class="pre">chrono_specs</span></code></a>] <a class="reference internal" href="#grammar-token-sf-conversion_spec"><code class="xref docutils literal notranslate"><span class="pre">conversion_spec</span></code></a> | <a class="reference internal" href="#grammar-token-sf-chrono_specs"><code class="xref docutils literal notranslate"><span class="pre">chrono_specs</span></code></a> <a class="reference internal" href="#grammar-token-sf-literal_char"><code class="xref docutils literal notranslate"><span class="pre">literal_char</span></code></a>
  429. <strong id="grammar-token-sf-conversion_spec"><span id="grammar-token-conversion-spec"></span>conversion_spec </strong> ::= &quot;%&quot; [<a class="reference internal" href="#grammar-token-sf-modifier"><code class="xref docutils literal notranslate"><span class="pre">modifier</span></code></a>] <a class="reference internal" href="#grammar-token-sf-chrono_type"><code class="xref docutils literal notranslate"><span class="pre">chrono_type</span></code></a>
  430. <strong id="grammar-token-sf-literal_char"><span id="grammar-token-literal-char"></span>literal_char </strong> ::= &lt;a character other than '{', '}' or '%'&gt;
  431. <strong id="grammar-token-sf-modifier"><span id="grammar-token-modifier"></span>modifier </strong> ::= &quot;E&quot; | &quot;O&quot;
  432. <strong id="grammar-token-sf-chrono_type"><span id="grammar-token-chrono-type"></span>chrono_type </strong> ::= &quot;a&quot; | &quot;A&quot; | &quot;b&quot; | &quot;B&quot; | &quot;c&quot; | &quot;C&quot; | &quot;d&quot; | &quot;D&quot; | &quot;e&quot; | &quot;F&quot; |
  433. &quot;g&quot; | &quot;G&quot; | &quot;h&quot; | &quot;H&quot; | &quot;I&quot; | &quot;j&quot; | &quot;m&quot; | &quot;M&quot; | &quot;n&quot; | &quot;p&quot; |
  434. &quot;q&quot; | &quot;Q&quot; | &quot;r&quot; | &quot;R&quot; | &quot;S&quot; | &quot;t&quot; | &quot;T&quot; | &quot;u&quot; | &quot;U&quot; | &quot;V&quot; |
  435. &quot;w&quot; | &quot;W&quot; | &quot;x&quot; | &quot;X&quot; | &quot;y&quot; | &quot;Y&quot; | &quot;z&quot; | &quot;Z&quot; | &quot;%&quot;
  436. </pre>
  437. <p>Literal chars are copied unchanged to the output. Precision is valid only for
  438. <code class="docutils literal notranslate"><span class="pre">std::chrono::duration</span></code> types with a floating-point representation type.</p>
  439. <p>The available presentation types (<em>chrono_type</em>) for chrono durations and time
  440. points are:</p>
  441. <table class="docutils align-default">
  442. <colgroup>
  443. <col style="width: 12%" />
  444. <col style="width: 88%" />
  445. </colgroup>
  446. <thead>
  447. <tr class="row-odd"><th class="head"><p>Type</p></th>
  448. <th class="head"><p>Meaning</p></th>
  449. </tr>
  450. </thead>
  451. <tbody>
  452. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'H'</span></code></p></td>
  453. <td><p>The hour (24-hour clock) as a decimal number. If the result is a
  454. single digit, it is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%OH</span></code>
  455. produces the locale’s alternative representation.</p></td>
  456. </tr>
  457. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'M'</span></code></p></td>
  458. <td><p>The minute as a decimal number. If the result is a single digit,
  459. it is prefixed with 0. The modified command <code class="docutils literal notranslate"><span class="pre">%OM</span></code> produces the
  460. locale’s alternative representation.</p></td>
  461. </tr>
  462. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'S'</span></code></p></td>
  463. <td><p>Seconds as a decimal number. If the number of seconds is less than
  464. 10, the result is prefixed with 0. If the precision of the input
  465. cannot be exactly represented with seconds, then the format is a
  466. decimal floating-point number with a fixed format and a precision
  467. matching that of the precision of the input (or to a microseconds
  468. precision if the conversion to floating-point decimal seconds
  469. cannot be made within 18 fractional digits). The character for the
  470. decimal point is localized according to the locale. The modified
  471. command <code class="docutils literal notranslate"><span class="pre">%OS</span></code> produces the locale’s alternative representation.</p></td>
  472. </tr>
  473. </tbody>
  474. </table>
  475. <p>Specifiers that have a calendaric component such as <code class="docutils literal notranslate"><span class="pre">'d'</span></code> (the day of month)
  476. are valid only for <code class="docutils literal notranslate"><span class="pre">std::tm</span></code> and not durations or time points.</p>
  477. </section>
  478. <section id="range-format-specifications">
  479. <h2>Range Format Specifications<a class="headerlink" href="#range-format-specifications" title="Permalink to this headline">¶</a></h2>
  480. <p>Format specifications for range types have the following syntax:</p>
  481. <pre>
  482. <strong id="grammar-token-sf-range_format_spec"><span id="grammar-token-range-format-spec"></span>range_format_spec</strong> ::= [&quot;:&quot; [<code class="xref docutils literal notranslate"><span class="pre">underlying_spec</span></code>]]
  483. </pre>
  484. <p>The <code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">underlying_spec</span></code> is parsed based on the formatter of the range’s
  485. reference type.</p>
  486. <p>By default, a range of characters or strings is printed escaped and quoted. But
  487. if any <code class="xref cpp cpp-any docutils literal notranslate"><span class="pre">underlying_spec</span></code> is provided (even if it is empty), then the characters
  488. or strings are printed according to the provided specification.</p>
  489. <p>Examples:</p>
  490. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="p">{</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">20</span><span class="p">,</span><span class="w"> </span><span class="mi">30</span><span class="p">});</span><span class="w"></span>
  491. <span class="c1">// Result: [10, 20, 30]</span>
  492. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{::#x}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="p">{</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">20</span><span class="p">,</span><span class="w"> </span><span class="mi">30</span><span class="p">});</span><span class="w"></span>
  493. <span class="c1">// Result: [0xa, 0x14, 0x13]</span>
  494. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="p">{</span><span class="sc">&#39;h&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;e&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;l&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;l&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;o&#39;</span><span class="p">});</span><span class="w"></span>
  495. <span class="c1">// Result: [&#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;]</span>
  496. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{::}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="p">{</span><span class="sc">&#39;h&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;e&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;l&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;l&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;o&#39;</span><span class="p">});</span><span class="w"></span>
  497. <span class="c1">// Result: [h, e, l, l, o]</span>
  498. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{::d}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">vector</span><span class="p">{</span><span class="sc">&#39;h&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;e&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;l&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;l&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;o&#39;</span><span class="p">});</span><span class="w"></span>
  499. <span class="c1">// Result: [104, 101, 108, 108, 111]</span>
  500. </pre></div>
  501. </div>
  502. </section>
  503. <section id="format-examples">
  504. <span id="formatexamples"></span><h2>Format Examples<a class="headerlink" href="#format-examples" title="Permalink to this headline">¶</a></h2>
  505. <p>This section contains examples of the format syntax and comparison with
  506. the printf formatting.</p>
  507. <p>In most of the cases the syntax is similar to the printf formatting, with the
  508. addition of the <code class="docutils literal notranslate"><span class="pre">{}</span></code> and with <code class="docutils literal notranslate"><span class="pre">:</span></code> used instead of <code class="docutils literal notranslate"><span class="pre">%</span></code>.
  509. For example, <code class="docutils literal notranslate"><span class="pre">&quot;%03.2f&quot;</span></code> can be translated to <code class="docutils literal notranslate"><span class="pre">&quot;{:03.2f}&quot;</span></code>.</p>
  510. <p>The new format syntax also supports new and different options, shown in the
  511. following examples.</p>
  512. <p>Accessing arguments by position:</p>
  513. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{0}, {1}, {2}&quot;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;a&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;b&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;c&#39;</span><span class="p">);</span><span class="w"></span>
  514. <span class="c1">// Result: &quot;a, b, c&quot;</span>
  515. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{}, {}, {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;a&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;b&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;c&#39;</span><span class="p">);</span><span class="w"></span>
  516. <span class="c1">// Result: &quot;a, b, c&quot;</span>
  517. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{2}, {1}, {0}&quot;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;a&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;b&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;c&#39;</span><span class="p">);</span><span class="w"></span>
  518. <span class="c1">// Result: &quot;c, b, a&quot;</span>
  519. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{0}{1}{0}&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;abra&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;cad&quot;</span><span class="p">);</span><span class="w"> </span><span class="c1">// arguments&#39; indices can be repeated</span>
  520. <span class="c1">// Result: &quot;abracadabra&quot;</span>
  521. </pre></div>
  522. </div>
  523. <p>Aligning the text and specifying a width:</p>
  524. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:&lt;30}&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;left aligned&quot;</span><span class="p">);</span><span class="w"></span>
  525. <span class="c1">// Result: &quot;left aligned &quot;</span>
  526. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:&gt;30}&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;right aligned&quot;</span><span class="p">);</span><span class="w"></span>
  527. <span class="c1">// Result: &quot; right aligned&quot;</span>
  528. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:^30}&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;centered&quot;</span><span class="p">);</span><span class="w"></span>
  529. <span class="c1">// Result: &quot; centered &quot;</span>
  530. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:*^30}&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;centered&quot;</span><span class="p">);</span><span class="w"> </span><span class="c1">// use &#39;*&#39; as a fill char</span>
  531. <span class="c1">// Result: &quot;***********centered***********&quot;</span>
  532. </pre></div>
  533. </div>
  534. <p>Dynamic width:</p>
  535. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:&lt;{}}&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;left aligned&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">30</span><span class="p">);</span><span class="w"></span>
  536. <span class="c1">// Result: &quot;left aligned &quot;</span>
  537. </pre></div>
  538. </div>
  539. <p>Dynamic precision:</p>
  540. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:.{}f}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mf">3.14</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
  541. <span class="c1">// Result: &quot;3.1&quot;</span>
  542. </pre></div>
  543. </div>
  544. <p>Replacing <code class="docutils literal notranslate"><span class="pre">%+f</span></code>, <code class="docutils literal notranslate"><span class="pre">%-f</span></code>, and <code class="docutils literal notranslate"><span class="pre">%</span> <span class="pre">f</span></code> and specifying a sign:</p>
  545. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:+f}; {:+f}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mf">3.14</span><span class="p">,</span><span class="w"> </span><span class="mf">-3.14</span><span class="p">);</span><span class="w"> </span><span class="c1">// show it always</span>
  546. <span class="c1">// Result: &quot;+3.140000; -3.140000&quot;</span>
  547. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{: f}; {: f}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mf">3.14</span><span class="p">,</span><span class="w"> </span><span class="mf">-3.14</span><span class="p">);</span><span class="w"> </span><span class="c1">// show a space for positive numbers</span>
  548. <span class="c1">// Result: &quot; 3.140000; -3.140000&quot;</span>
  549. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:-f}; {:-f}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mf">3.14</span><span class="p">,</span><span class="w"> </span><span class="mf">-3.14</span><span class="p">);</span><span class="w"> </span><span class="c1">// show only the minus -- same as &#39;{:f}; {:f}&#39;</span>
  550. <span class="c1">// Result: &quot;3.140000; -3.140000&quot;</span>
  551. </pre></div>
  552. </div>
  553. <p>Replacing <code class="docutils literal notranslate"><span class="pre">%x</span></code> and <code class="docutils literal notranslate"><span class="pre">%o</span></code> and converting the value to different bases:</p>
  554. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">);</span><span class="w"></span>
  555. <span class="c1">// Result: &quot;int: 42; hex: 2a; oct: 52; bin: 101010&quot;</span>
  556. <span class="c1">// with 0x or 0 or 0b as prefix:</span>
  557. <span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">);</span><span class="w"></span>
  558. <span class="c1">// Result: &quot;int: 42; hex: 0x2a; oct: 052; bin: 0b101010&quot;</span>
  559. </pre></div>
  560. </div>
  561. <p>Padded hex byte with prefix and always prints both hex characters:</p>
  562. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;{:#04x}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
  563. <span class="c1">// Result: &quot;0x00&quot;</span>
  564. </pre></div>
  565. </div>
  566. <p>Box drawing using Unicode fill:</p>
  567. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="w"></span>
  568. <span class="w"> </span><span class="s">&quot;┌{0:─^{2}}┐</span><span class="se">\n</span><span class="s">&quot;</span><span class="w"></span>
  569. <span class="w"> </span><span class="s">&quot;│{1: ^{2}}│</span><span class="se">\n</span><span class="s">&quot;</span><span class="w"></span>
  570. <span class="w"> </span><span class="s">&quot;└{0:─^{2}}┘</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Hello, world!&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">20</span><span class="p">);</span><span class="w"></span>
  571. </pre></div>
  572. </div>
  573. <p>prints:</p>
  574. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span>┌────────────────────┐
  575. │ Hello, world! │
  576. └────────────────────┘
  577. </pre></div>
  578. </div>
  579. <p>Using type-specific formatting:</p>
  580. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;fmt/chrono.h&gt;</span><span class="cp"></span>
  581. <span class="k">auto</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tm</span><span class="p">();</span><span class="w"></span>
  582. <span class="n">t</span><span class="p">.</span><span class="n">tm_year</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2010</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1900</span><span class="p">;</span><span class="w"></span>
  583. <span class="n">t</span><span class="p">.</span><span class="n">tm_mon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">7</span><span class="p">;</span><span class="w"></span>
  584. <span class="n">t</span><span class="p">.</span><span class="n">tm_mday</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"></span>
  585. <span class="n">t</span><span class="p">.</span><span class="n">tm_hour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">12</span><span class="p">;</span><span class="w"></span>
  586. <span class="n">t</span><span class="p">.</span><span class="n">tm_min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">15</span><span class="p">;</span><span class="w"></span>
  587. <span class="n">t</span><span class="p">.</span><span class="n">tm_sec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">58</span><span class="p">;</span><span class="w"></span>
  588. <span class="n">fmt</span><span class="o">::</span><span class="n">print</span><span class="p">(</span><span class="s">&quot;{:%Y-%m-%d %H:%M:%S}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="p">);</span><span class="w"></span>
  589. <span class="c1">// Prints: 2010-08-04 12:15:58</span>
  590. </pre></div>
  591. </div>
  592. <p>Using the comma as a thousands separator:</p>
  593. <div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;fmt/format.h&gt;</span><span class="cp"></span>
  594. <span class="k">auto</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fmt</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">locale</span><span class="p">(</span><span class="s">&quot;en_US.UTF-8&quot;</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;{:L}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">1234567890</span><span class="p">);</span><span class="w"></span>
  595. <span class="c1">// s == &quot;1,234,567,890&quot;</span>
  596. </pre></div>
  597. </div>
  598. </section>
  599. </section>
  600. </div>
  601. </div>
  602. </div>
  603. <div class="footer" role="contentinfo">
  604. &copy; Copyright 2012-present, Victor Zverovich.
  605. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 3.3.0.
  606. </div>
  607. <script src="_static/bootstrap.min.js"></script>
  608. </body>
  609. </html>