Home » The formatting library in C++20: Details about the format string

The formatting library in C++20: Details about the format string

by admin
The formatting library in C++20: Details about the format string

The formatting library in C++20: Details about the format string

In my last article “The Formatting Library in C++20: The Format String” I introduced some of the format specifications of the format string. Today I am finishing the essay on this.

Advertisement

Rainer Grimm has been working as a software architect, team and training manager for many years. He enjoys writing articles on the programming languages ​​C++, Python and Haskell, but also enjoys speaking frequently at specialist conferences. On his blog Modern C++ he deals intensively with his passion C++.

In today’s article I will write about the width, precision and data type of the format specification. If you want to know more about the id argument, padding, alignment, signs and alternative form, read my previous article: “The Formatting Library in C++20: The Format String”.

You can specify the width and precision of an argument. Width can be applied to numbers and precision can be applied to floating point numbers and strings. For floating point numbers, precision indicates the formatting precision; For strings, precision indicates how many characters are used and how the string is ultimately truncated. If the precision is greater than the length of the string, it has no effect on the string.

Width: You can use either a positive decimal number or a replacement field ({} or {n}). If specified, n indicates the minimum width.Precision: One can use a period (.) followed by a non-negative decimal number or a replacement field.

A few examples should help you understand the basics:

// formatWidthPrecision.cpp

See also  technical data sheet and price of the first BigG leaflet

#include #include
#include

int main() {

int i = 123456789;
double d = 123.456789;

std::cout The w character in the source code represents the width; also the p-sign for accuracy.

Here are a few interesting observations about the program: No extra spaces are added when the width is specified with a replacement field (1). If a precision greater than the length of the displayed double (2 and 3) is used, the length of the displayed value reflects the precision. This observation does not apply to a string (4 and 5).

In addition, the width and accuracy can be parameterized.

// formatWidthPrecisionParametrized.cpp

#include #include

int main() {

std::cout The program formatWidthPrecisionParametrized.cpp represents the double doub in various ways. (1) applies the default setting. (2) the precision varies from 3 to 9. The last argument of the format string goes into the inner {} of the format specifier {:. {}}. Finally, in (3), the width of the displayed double values ​​is set to 10.

Generally, the compiler infers the type of the value used. But sometimes you want to specify the data type. These are the main data types:

Strings: s

Integers:

b: binary formatB: same as b, but base prefix is ​​0Bd: decimal formato: octal formatx: hexadecimal formatX: same as x, but base prefix is ​​0X

char and wchar_t:

b, B, d, o, x, X: like integers

bool:

s: true or falseb, B, d, o, x, X: like integers

Floating point numbers:

e: exponential formatE: like e, but the exponent is written with Ef, F: fixed point; the precision is 6g, G: precision 6, but the exponent is written with E

See also  5 ways to personalize your Android smartphone using AI

pointer:

p: hexadecimal notation of its address

Only the void, const void and std::nullptr_t data types are valid. If you want to display the address of an arbitrary pointer, you have to convert it to (const) void*.

double d = 123.456789;

std::format(“{}”, &d); // ERROR
std::format(“{}”, static_cast(&d)); // okay
std::format(“{}”, static_cast(&d)); // okay
std::format(“{}”, nullptr); // okay

With the data types you can easily represent an int in a different number system.

// formatType.cpp

#include #include

int main() {

int num{2020};

std::cout What’s next?

So far I have formatted the elementary data types and strings. Of course, custom types can also be formatted. That will be the topic of my next article. (rme)

To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy