是否有一个内置的功能在C,C ++或JavaScript的逗号分隔多少? [英] Is there a built-in function that comma-separates a number in C, C++, or JavaScript?
本文介绍了是否有一个内置的功能在C,C ++或JavaScript的逗号分隔多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于一些 12456789
,我需要输出 12456789
没有太多的编码。是否有任何内置函数使用C,C ++或JavaScript我可以用它来做到这一点?
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?
推荐答案
我发现这个小JavaScript函数,将工作(的来源):
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;
}
这篇关于是否有一个内置的功能在C,C ++或JavaScript的逗号分隔多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文