c++: Format number with commas? -


I want to type in a method that will take an integer and std :: string this integer comma Is formatted with.

Example Announcement:

  std :: string FormatWithCommas (long value);   

Example Usage:

  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"   

C ++ method of formatting a number string

(The bonus will also have to handle double s).

Experiment with

  #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

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

c# - Confused over DLL entry points (entry point not found exception) -