Is there a built-in function that comma splits a number into C, C ++ or JavaScript?

Given the number 12456789 , I need to output 12,456,789 without a lot of encoding. Are there any built-in functions in C, C ++ or JavaScript that I can use for this?

+1
c ++ c javascript
Aug 13 '10 at 17:56
source share
6 answers

I found this little javascript function that will work ( source ):

 function addCommas(nStr){ nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; } 
+2
Aug 13 '10 at 18:10
source share

Yes, this can be done automatically in C ++ by setting the correct face in the locale.

 #include <iostream> #include <locale> #include <string> template<typename CharT> struct Sep : public std::numpunct<CharT> { virtual std::string do_grouping() const {return "\003";} }; int main() { std::cout.imbue(std::locale(std::cout.getloc(), new Sep <char>())); std::cout << 123456789 << "\n"; } 

Note. C-locale (the locale used when your application is not specifically configured) does not use the thousands separator. If you set the language standard of your application to a specific language, then it will select the language that corresponds to the grouping method (without having to do something like the above). If you want to set the locale to what your current language settings are for your computer (as defined by the OS), and not a specific language, then use "" (blank line) as the locale.

So, to set the locale based on the specific settings of your OS:

 int main() { std::cout.imbue(std::locale("")); std::cout << 123456789 << "\n"; } 
+21
Aug 13 '10 at 18:01
source share

In C ++, you usually use something like this:

 std::locale loc(""); std::cout.imbue(loc); std::cout << 1234567; 

A locale with an empty name like this will not necessarily format the number exactly as described above. Instead, he selects a locale from the rest of the system and formats it accordingly, so for me (with my system configured for the USA) it will produce "1,234,567", but if the system was configured for (most parts) Europe, then it will produce " 1.234.567 ".

+14
Aug 13 '10 at 18:20
source share

In some implementations of the C compiler and extension, printf familiy functions are provided, so the single quote / apostrophe character used as a modifier in a number format specifier will perform a "grouping of thousands":

 #include <stdio.h> #include <locale.h> int main(void) { printf( "%'d\n", 1234567); setlocale(LC_NUMERIC, "en_US"); printf( "%'d\n", 1234567); return 0; } 

will produce (with GCC 4.4.1 anyway):

 1234567 1,234,567 

Unfortunately, this extension is not particularly widely supported.

+3
Aug 13 '10 at 18:12
source share

// Another javascript method working with numbers

 Number.prototype.withCommas= function(){ return String(this).replace(/\B(?=(?:\d{3})+(?!\d))/g,',') } var n=12456789.25; alert(n.withCommas()); 

/ * return value: (String) 12,456,789.25 * /

+2
Aug 13 2018-10-10T00:
source share

For C ++ you can try:

 std::locale get_numpunct_locale(std::locale locale = std::locale("")) { #if defined(_MSC_VER) && defined(_ADDFAC) && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 403) // Workaround for older implementations std::_ADDFAC(locale, new std::numpunct<char>()); return locale; #else return std::locale(locale, new std::numpunct<TCHAR>()); #endif } template<class T> std::string nformat(T value) { std::ostringstream ss; ss.imbue(get_numpunct_locale()); ss << value; return ss.str(); } 
+1
Sep 07
source share



All Articles