Click here to Skip to main content
15,886,714 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
in C++ 20, there is a new method for formatting strings, called std::format.
However, its hot clear how (and would it be possible) to format the output, especially float values where you would want to control how many digits after the decimal point, should be printed.

What I have tried:

Using { and } (for example std::format("The value of X is {}", x) but {} isn't specific for what and how should be formatted.
Posted
Updated 17-Aug-22 5:41am

I've only skimmed this article, but you might find it helpful:

An Extraterrestrial Guide to C++20 Text Formatting - C++ Stories[^]

If there's already a standard way to do it, that's what I'd use. So far, I've found little use for all the things added after C++11.
 
Share this answer
 
Comments
[no name] 15-Aug-22 9:33am    
" I've found little use for all the things added after C++11." - interesting point of view...
Greg Utas 15-Aug-22 20:54pm    
I'm an opinionated bastard. :)

By the way, the site that Richard linked to below is the one I use when I need to look something up about C++ or C. It's much more of a reference site than a teaching site, however.
[no name] 15-Aug-22 21:26pm    
thanks
[no name] 16-Aug-22 13:32pm    
Greg, actually I am in a need to use std::format, and just want to be sure: does it only support {} when parameters are added?
Greg Utas 16-Aug-22 13:39pm    
I've never used it, but the page that Richard linked to should give you the answer.
See std::formatter - cppreference.com[^] which explains all the fields is a format selector.
 
Share this answer
 
Comments
[no name] 16-Aug-22 13:32pm    
What is the difference between std::formatter and std::format?
Richard MacCutchan 16-Aug-22 14:33pm    
Follow the link I gave you, the documentation explains it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900