js 数字转千分位 简单点的
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 数字转千分位 简单点的相关的知识,希望对你有一定的参考价值。
比如 12345转12,345.00
方法1:(123456789.2333).toLocaleString('en-US');
方法2:
function format(input)
var n = parseFloat(input).toFixed(2);
var re = /(\\d1,3)(?=(\\d3)+(?:\\.))/g;
return n.replace(re, "$1,");
format(123456789.2333) 参考技术A function format (num)
return (num.toFixed(2) + '').replace(/\\d1,3(?=(\\d3)+(\\.\\d*)?$)/g, '$&,');
var num = 12345;
alert(format(num));本回答被提问者采纳 参考技术B "12345".replace(/^(\\d+)((\\.\\d+)?)$/, function(v1, v2, v3)
return v2.replace(/\\d1,3(?=(\\d3)+$)/g, '$&,') + (v3 || '.00');
);追问
这个逻辑看不明白,,貌似这样写不对吧?
追答对不对的话,你自己做个测试不就一清二楚了么。这个应该是最简单的方法了。
参考技术C <script>var total = 12345;
total= total.toFixed(3);
alert(total)
</script>
数字字符串转千分位数字字符串
//将数字字符串转为千分位格式
inline
string
number_to_currency(string
str, bool
bDot = true)
{
????string
newStr = "";
????int
count = 0;
????if (str.find(".") == string::npos) //
????{
????????for (int
i = str.length() - 1; i >= 0; i--)
????????{
????????????string
tmp = "a";
????????????tmp[0] = str.at(i);
????????????if (count % 3 == 0 && count != 0)
????????????{
????????????????newStr = tmp + "," + newStr;
????????????}
????????????else
????????????{
????????????????newStr = tmp + newStr;
????????????}
????????????count++;
????????}
????????if (bDot)
????????????newStr = newStr + ".00"; //自动补小数点后两位
????}
????else
????{
????????for (int
i = str.find(".") - 1; i >= 0; i--)
????????{
????????????string
tmp = "a";
????????????tmp[0] = str.at(i);
????????????if (count % 3 == 0 && count != 0)
????????????{
????????????????newStr = tmp + "," + newStr; //碰到3的倍数则加上","号
????????????}
????????????else
????????????{
????????????????newStr = tmp + newStr; //逐个字符相接起来
????????????}
????????????count++;
????????}
????????newStr = newStr + (str + "00").substr((str + "00").find("."), 3);
????}
????return
newStr;
}
?
以上是关于js 数字转千分位 简单点的的主要内容,如果未能解决你的问题,请参考以下文章