C++20 String Formatting: Part-4 Number type formatting

Gajendra Gulgulia
7 min readMar 10, 2023

In the third part of the series we looked at how to format signs of numbers with the string formatting library. In the fourth part of the series, we’ll look at Type format specifier that applies to numeric as well as some fundamental non-numeric arguments. Further we’ll also look at how the optional specifier 0 and # change the formatted output of the arguments.

In this part of the series, only formatting of numeric arguments will be covered. In the fifth part of the series, formatting of non-numeric arguments will be covered.

1. Introduction

Numbers have types like integral and floating point types. Further in each of the two types, there can be multiple standard representations like binary, octal, decimal, hexadecimal, scientific, fixed, etc. Here’s a table for summary to understand what representations are possible (and sensible) for integral numbers and for floating point numbers in general. (see NOTE1)

This is exactly what the Type format specifier intends to capture in the formatting library and allow for flexible representation of numbers in different format suitable for different applications. The type format specifier appears at the end in the entire list of format specification like for e.g alignment, sign, width, precision as can be seen in the image below

--

--