C++20 String Formatting: Part-3 Sign specification
In the second part of the tutorial series, we looked at the general structure of format specifiers and later looked into the details of how to specify the width, fill and alignment in the string formatting library. In this part of the series, we will look at how to specify the sign ( +
, -
) (aka the sign format specifier) to numbers (marked green in the image below).
1. Introduction
Before diving deep in this topic, think how signs are naturally used in numbers when numbers are used in programs, documents, tables etc:
- Positive Numbers: positive numbers are not specified with any sign
- Negative Numbers: usually a minus sign (
-
) is used before the number to indicate the negative property of the number.
From this we can summarize that, we, humans have a default way of specifying signs to numbers: only negative numbers have -
sign applied to it while non-negative numbers including zeros have no +
sign applied to it.
In C++20 string formatting library, a similar convention is used which is coherent with how, we, humans, are used to represent numbers.
According to cpp reference page, the sign option can be one of following:
+
: Indicates that a sign should be used for both…