c++: Format number with commas? -
I want to type in a method that will take an integer and Example Announcement: Example Usage: C ++ method of formatting a number (The bonus will also have to handle Experiment with std :: string this integer comma Is formatted with.
std :: string FormatWithCommas (long value);
std :: string result = FormatWithCommas (7800); Std :: string result2 = FormatWithCommas (5100100); Std :: string result3 = FormatWithCommas (201234567890); // result = "7,800" // result 2 = "5,100,100" // result 3 = "201,234,567,890"
string
double s).
#include & lt; Iomanip & gt; #include & lt; Locale & gt; Template & lt; Class T & gt; Std :: string FormatWithCommas (T value) {std :: stringstream ss; Ss.imbue (std :: location ("")); SS & lt; & Lt; Std :: Fixed & lt; & Lt; value; Return ss.str (); Disclaimer: Portability can be a problem and you should probably see which location is used when
"" Is passed
Comments
Post a Comment