SyntaxFix
Write A Post
Hire A Developer
Questions
Another option is snprintf:
snprintf
double pi = 3.1415926; std::string s(16, '\0'); auto written = std::snprintf(&s[0], s.size(), "%.2f", pi); s.resize(written);
Demo. Error handling should be added, i.e. checking for written < 0.
written < 0