{"id":9057,"date":"2024-02-05T10:40:23","date_gmt":"2024-02-05T10:40:23","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9057"},"modified":"2024-02-05T10:47:19","modified_gmt":"2024-02-05T10:47:19","slug":"the-formatting-library-in-c20-the-format-string-2","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/the-formatting-library-in-c20-the-format-string-2\/","title":{"rendered":"The Formatting Library in C++20: The Format String (2)"},"content":{"rendered":"\n<p>In my last post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/the-formatting-library-in-c20-the-format-string\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/the-formatting-library-in-c20-the-format-string\/\">The Formatting Library in C++20: The Format String<\/a>&#8220;, I presented a few of the format specifications of the format string. Today, I finish my job.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"955\" height=\"392\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/01\/TimelineCpp20CoreLanguage.png\" alt=\"\" class=\"wp-image-8964\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/01\/TimelineCpp20CoreLanguage.png 955w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/01\/TimelineCpp20CoreLanguage-300x123.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/01\/TimelineCpp20CoreLanguage-768x315.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/01\/TimelineCpp20CoreLanguage-705x289.png 705w\" sizes=\"auto, (max-width: 955px) 100vw, 955px\" \/><\/figure>\n\n\n\n<p>In today&#8217;s post, I will write about the width, precision, and type of the format specification. If you want to know more about the argument id, fill characters, alignment, signess, and the alternative form, read my previous post: &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/the-formatting-library-in-c20-the-format-string\/\">The Formatting Library in C++20: The Format String<\/a>&#8220;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Width and  Precision<\/h2>\n\n\n\n<p>You can specify the width and the precision of your argument. The width specifier can be applied to numbers, and the precision to floating-point numbers and strings. For floating-point types, the precision specifies the formatting precision; for strings, the precision specifies how many characters are used and, ultimately, trimming the string. It does not affect a string if the precision is greater than the length of the string.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>width: you can use either a positive decimal number or a replacement field (<code>{}<\/code> or <code>{n}<\/code>). When given, <code>n <\/code>specifies the minimum width.<\/li>\n\n\n\n<li>precision: you can use a period (<code>.)<\/code> followed by a non-negative decimal number or a replacement field.<\/li>\n<\/ul>\n\n\n\n<p>A few examples should help you grasp the basics:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ formatWidthPrecision.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;format&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> i <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">123456789<\/span>;\n    <span style=\"color: #007788; font-weight: bold\">double<\/span> d <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">123.456789<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, i) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:15}&quot;<\/span>, i) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>; <span style=\"color: #0099FF; font-style: italic\">\/\/ (w = 15)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:}&quot;<\/span>, i) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;   <span style=\"color: #0099FF; font-style: italic\">\/\/ (w = 15)             \/\/ (1)<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, d) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:15}&quot;<\/span>, d) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>; <span style=\"color: #0099FF; font-style: italic\">\/\/ (w = 15)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:}&quot;<\/span>, d) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;   <span style=\"color: #0099FF; font-style: italic\">\/\/ (w = 15)<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>string s<span style=\"color: #555555\">=<\/span> <span style=\"color: #CC3300\">&quot;Only a test&quot;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:10.50}&quot;<\/span>, d) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>; <span style=\"color: #0099FF; font-style: italic\">\/\/ (w = 10, p = 50)   \/\/ (2)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:{}.{}}&quot;<\/span>, d, <span style=\"color: #FF6600\">10<\/span>, <span style=\"color: #FF6600\">50<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;  <span style=\"color: #0099FF; font-style: italic\">\/\/ (w = 10,  \/\/ (3)<\/span>\n                                                                         <span style=\"color: #0099FF; font-style: italic\">\/\/  p = 50)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:10.5}&quot;<\/span>, d) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;  <span style=\"color: #0099FF; font-style: italic\">\/\/ (w = 10, p = 5)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:{}.{}}&quot;<\/span>, d, <span style=\"color: #FF6600\">10<\/span>, <span style=\"color: #FF6600\">5<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;  <span style=\"color: #0099FF; font-style: italic\">\/\/ (w = 10,<\/span>\n                                                                         <span style=\"color: #0099FF; font-style: italic\">\/\/  p = 5)<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:.500}&quot;<\/span>, s) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;      <span style=\"color: #0099FF; font-style: italic\">\/\/ (p = 500)      \/\/ (4)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:.{}}&quot;<\/span>, s, <span style=\"color: #FF6600\">500<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;  <span style=\"color: #0099FF; font-style: italic\">\/\/ (p = 500)      \/\/ (5)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:.5}&quot;<\/span>, s) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;---<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;        <span style=\"color: #0099FF; font-style: italic\">\/\/ (p = 5)<\/span>\n\n}\n<\/pre><\/div>\n\n\n\n<p>The<code> w<\/code> character in the source code stands for the width; similarly, the <code>p<\/code> character for the precision. I have a few interesting observations about the program. No extra spaces are added when you specify the width with a replacement field (line 1). When you specify a precision higher than the length of the displayed <code>double<\/code> (lines 2 and 3), the length of the displayed value reflects the precision. This observation does not hold for a string (lines 4 and 5).<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"421\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/formatWidthPrecision.png\" alt=\"\" class=\"wp-image-9064\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/formatWidthPrecision.png 572w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/formatWidthPrecision-300x221.png 300w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/figure>\n\n\n\n<p>Additionally, you can also parametrize the width and the precision.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ formatWidthPrecisionParametrized.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;format&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #007788; font-weight: bold\">double<\/span> doub <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">123.456789<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, doub);                          <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> precision<span style=\"color: #555555\">:<\/span> {<span style=\"color: #FF6600\">3<\/span>, <span style=\"color: #FF6600\">5<\/span>, <span style=\"color: #FF6600\">7<\/span>, <span style=\"color: #FF6600\">9<\/span>}) {\n       std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:.{}}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, doub, precision);         <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n    }\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> width <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">10<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #006699; font-weight: bold\">auto<\/span> precision<span style=\"color: #555555\">:<\/span> {<span style=\"color: #FF6600\">3<\/span>, <span style=\"color: #FF6600\">5<\/span>, <span style=\"color: #FF6600\">7<\/span>, <span style=\"color: #FF6600\">9<\/span>}) {\n       std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:{}.{}}<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>, doub, width, precision); <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    }\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p>The program<code> formatWidthPrecisionParametrized.cpp <\/code>displays the double <code>doub <\/code>in various ways. Line (1) applies the default. Line (2) varies  <code>precision<\/code> from 3 to 9. The last argument of the format string goes into the inner <code>{}<\/code> of the format specifier<code> {:.{}}<\/code>. Finally, line (3) sets the width of the displayed doubles to 10. \u00a0<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"547\" height=\"381\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/formatWidthPrecisionParametrized.png\" alt=\"\" class=\"wp-image-9066\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/formatWidthPrecisionParametrized.png 547w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/formatWidthPrecisionParametrized-300x209.png 300w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Type<\/h2>\n\n\n\n<p>In general, the compiler deduces the type of the value used. But sometimes, you want to specify the type. These are the most important type specifications:<\/p>\n\n\n\n<p><strong>Strings<\/strong>:<code> s<\/code><\/p>\n\n\n\n<p><strong>Integers<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>b<\/code>: binary format<\/li>\n\n\n\n<li><code>B<\/code>: same as <code>b<\/code>, but the base prefix is <code>0B<\/code><\/li>\n\n\n\n<li><code>d<\/code>: decimal format<\/li>\n\n\n\n<li><code>o<\/code>: octal format<\/li>\n\n\n\n<li><code>x<\/code>: hexadecimal format<\/li>\n\n\n\n<li><code>X<\/code>: same as <code>x<\/code>, but the base prefix is <code>0X<\/code><\/li>\n<\/ul>\n\n\n\n<p><code><strong>char<\/strong><\/code> and <strong><code>wchar_t<\/code><\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&nbsp;<code>b, B, d, o, x, X<\/code>: such as integers<\/li>\n<\/ul>\n\n\n\n<p><strong><code>bool<\/code><\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>s: <code>true<\/code> or <code>false<\/code><\/li>\n\n\n\n<li><code>b, B, d, o, x, X<\/code>: such as integers<\/li>\n<\/ul>\n\n\n\n<p><strong>Floating-point<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>e<\/code>: exponential format<\/li>\n\n\n\n<li><code>E<\/code>: same as <code>e<\/code>, but the exponent is written with <code>E<\/code><\/li>\n\n\n\n<li><code>f, F<\/code>: fixed point; precision is 6<\/li>\n\n\n\n<li><code>g, G<\/code>: precision 6, but the exponent is written with <code>E<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Pointer<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>p<\/code>: hexadecimal notation of its address<\/li>\n<\/ul>\n\n\n\n<p>Only <code>void<\/code>,<code> const void<\/code>, and <code>std::nullptr_t<\/code> pointer types are valid. If you want to display the address of an arbitrary pointer, you must cast it to <code>(const) void*<\/code>.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #007788; font-weight: bold\">double<\/span> d <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">123.456789<\/span>;\n\nstd<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, <span style=\"color: #555555\">&amp;<\/span>d);                           <span style=\"color: #0099FF; font-style: italic\">\/\/ ERROR<\/span>\nstd<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, <span style=\"color: #006699; font-weight: bold\">static_cast<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">void<\/span><span style=\"color: #555555\">*&gt;<\/span>(<span style=\"color: #555555\">&amp;<\/span>d));       <span style=\"color: #0099FF; font-style: italic\">\/\/ okay<\/span>\nstd<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, <span style=\"color: #006699; font-weight: bold\">static_cast<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #007788; font-weight: bold\">void<\/span><span style=\"color: #555555\">*&gt;<\/span>(<span style=\"color: #555555\">&amp;<\/span>d)); <span style=\"color: #0099FF; font-style: italic\">\/\/ okay<\/span>\nstd<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{}&quot;<\/span>, nullptr);                      <span style=\"color: #0099FF; font-style: italic\">\/\/ okay<\/span>\n<\/pre><\/div>\n\n\n\n<p>The type specifiers allow you to display an <code>int<\/code> in a different number system.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ formatType.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;format&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> num{<span style=\"color: #FF6600\">2020<\/span>};\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;default:     &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:}&quot;<\/span>, num) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;decimal:     &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:d}&quot;<\/span>, num) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;binary:      &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:b}&quot;<\/span>, num) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;octal:       &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:o}&quot;<\/span>, num) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;hexadecimal: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:x}&quot;<\/span>, num) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"379\" height=\"216\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/FormatType.png\" alt=\"\" class=\"wp-image-9070\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/FormatType.png 379w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/FormatType-300x171.png 300w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p>So far, I&#8217;ve formatted basic types and strings. Additionally, you can format user-defined types. This will be the topic of my next post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last post, &#8220;The Formatting Library in C++20: The Format String&#8220;, I presented a few of the format specifications of the format string. Today, I finish my job. In today&#8217;s post, I will write about the width, precision, and type of the format specification. If you want to know more about the argument id, [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":8964,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[454],"class_list":["post-9057","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-format"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=9057"}],"version-history":[{"count":15,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9057\/revisions"}],"predecessor-version":[{"id":9077,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9057\/revisions\/9077"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8964"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=9057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}