开发者

Is there a built-in function that comma-separates a number in C, C++, or JavaScript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking 开发者_开发问答us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 8 years ago.

Improve this question

Given a number 12456789, I need to output 12,456,789 without much coding. Are there any built-in functions in either C, C++, or JavaScript I can use to do that?


Yes this can be done automatically in C++ by setting the correct facet on 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: The C-locale (The locale used when your application does not specifically set one) does not use a thousands separator. If you set the locale of your application to a specific language then it will pick up the languages specific method of grouping (without having to do anything fancy like the above). If you want to set the locale to what your machines current language settings (as defined by the OS) rather than a specific locale then use "" (empty string) as the locale.

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

int main()
{
    std::cout.imbue(std::locale(""));

    std::cout << 123456789 << "\n";
}


In C++ you'd typically use something like this:

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

std::cout << 1234567;

The locale with an empty name like this uses won't necessarily format the number exactly as you've specified above. Instead, it picks up the locale from the rest of the system, and formats appropriately, so for me (with my system set up for the US) it would produce "1,234,567", but if the system was set up for (most parts of) Europe, it would produce "1.234.567" instead.


In some C compiler implementations, and extension is provided for the printf familiy of functions such that a single-quote/apostrophe character used as a modifier in a numeric format specifier will perform 'thousands grouping':

#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 isn't particularly widely supported.


// Another javascript method, works on numbers

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

var n=12456789.25;
alert(n.withCommas());

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


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();
}


I found this little javascript function that would 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜