js如何实现三位数字逗号间隔?如100,000

gooddays 2007-06-21 09:10:02
请教实现数字的三位逗号间隔怎么实现,有函数吗?给个demo吧.
...全文
1754 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
muxrwc 2007-06-21
  • 打赏
  • 举报
回复
在不知这个函数前写过一个函数。。

<script type="text/javascript">
Number.prototype.$toString = function (num) {
var wc = "" + this, s = wc.match(/^(\d+)(\.\d+)?$/);
return s[1].split("").reverse().join("").replace(/(\d{3})(?=\d)/g, "$1,").split("").reverse().join("") +
s[2].substring(0, num ? num + 1 : s[2].length);
};

String.prototype.$toString = function (num) {
var wc = "" + Number(this), s = wc.match(/^(\d+)(\.\d+)?$/);
return s ? s[1].split("").reverse().join("").replace(/(\d{3})(?=\d)/g, "$1,").split("").reverse().join("") +
s[2].substring(0, num ? num + 1 : s[2].length) : "";
};

var show = function (s, n) {
alert(s.$toString(n));
};

show(12345678.12345678, 2);
show("012345678.12345678", 2);
</script>
muxrwc 2007-06-21
  • 打赏
  • 举报
回复
<script type="text/javascript">
var num_s = "1232134456.546";
alert(parseFloat(num_s).toLocaleString());
</script>
jinjuduo 2007-06-21
  • 打赏
  • 举报
回复
这是梅花雪写的,我收藏的,转帖出来
<script>
//引用梅花雪的代码
//将数字转换成三位逗号分隔的样式

alert(formatNum(-12345678.123));
alert(formatNum("12345678.123"));
alert(formatNum("10000000000000000000000000000000000000000"));
function formatNum(num)
{
if(!/^(\+|-)?\d+(\.\d+)?$/.test(num))
{alert("wrong!");
return num;}
var re = new RegExp().compile("(\\d)(\\d{3})(,|\\.|$)");
num += "";
while(re.test(num))
num = num.replace(re, "$1,$2$3")
return num;
}
</script>
FuWaer 2007-06-21
  • 打赏
  • 举报
回复
看了hookee的,自己忘记金额可以是小数了,补充上
a.html
--------------------------------------------------------------------
<html>
<script language=javascript>
function init()
{
alert(formatCash(1234567.1234567));
}
function formatCash( cash )
{
var str_cash = cash + "";//转换成字符串
var ret_cash = "";
var counter = 0;
for(var i=str_cash.length-1;i>=0;i--)
{
ret_cash = str_cash.charAt(i) + ret_cash;
if(str_cash.charAt(i)==".")
{
counter = 0;
continue;
}
counter++;
if(counter==3)
{
counter = 0;
if(i!=0&&str_cash.charAt(i-1)!=".")
ret_cash = "," + ret_cash;
}

}
return ret_cash;
}
</script>
<body onload=init()>
</body>
</html>

--------------------------------------------------------------------
hookee 2007-06-21
  • 打赏
  • 举报
回复
FormatNumber(123.445,2,true,false,true)

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
IN:
NUM - the number to format
decimalNum - the number of decimal places to format the number to
bolLeadingZero - true / false - display a leading zero for
numbers between -1 and 1
bolParens - true / false - use parenthesis around negative numbers
bolCommas - put commas as number separators.

RETVAL:
The formatted number!
**********************************************************************/
{
if (isNaN(parseInt(num))) return "NaN";

var tmpNum = num;
var iSign = num < 0 ? -1 : 1; // Get sign of number

// Adjust number so only the specified number of numbers after
// the decimal point are shown.
tmpNum *= Math.pow(10,decimalNum);
tmpNum = Math.round(Math.abs(tmpNum))
tmpNum /= Math.pow(10,decimalNum);
tmpNum *= iSign; // Readjust for sign


// Create a string object to do our formatting on
var tmpNumStr = new String(tmpNum);

// See if we need to strip out the leading zero or not.
if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
if (num > 0)
tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
else
tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);

// See if we need to put in the commas
if (bolCommas && (num >= 1000 || num <= -1000)) {
var iStart = tmpNumStr.indexOf(".");
if (iStart < 0)
iStart = tmpNumStr.length;

iStart -= 3;
while (iStart >= 1) {
tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
iStart -= 3;
}
}

// See if we need to use parenthesis
if (bolParens && num < 0)
tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

return tmpNumStr; // Return our formatted string!
}
FuWaer 2007-06-21
  • 打赏
  • 举报
回复
a.html
----------------------------------------------------------------------------
<html>
<script language=javascript>
function init()
{
alert(formatCash(1234567));
}
function formatCash( cash )
{
var str_cash = cash + "";//转换成字符串
var ret_cash = "";
var counter = 0;
for(var i=str_cash.length-1;i>=0;i--)
{
ret_cash = str_cash.charAt(i) + ret_cash;
counter++;
if(counter==3)
{
counter = 0;
if(i!=0)
ret_cash = "," + ret_cash;
}
}
return ret_cash;
}
</script>
<body onload=init()>
</body>
</html>
------------------------------------------------------------------------------------
kindwell 2007-06-21
  • 打赏
  • 举报
回复
还是自己写函数吧,for循环加上substr应该可以搞定吧?
dh20156 2007-06-21
  • 打赏
  • 举报
回复
vbs里可以用formatnumber,JS不知道有米类似的函数!
gooddays 2007-06-21
  • 打赏
  • 举报
回复
gooddays 2007-06-21
  • 打赏
  • 举报
回复
jinjuduo的办法帮我解决了,多谢大家
rjzou2006 2007-06-21
  • 打赏
  • 举报
回复
看我的


//---------------------------------------------------------------------------
// 表现形式增加逗号,这个可作为单独使用。
// Creation date: (2003-09-12)
// @author: ecc-wangdong,handong
// @version: 1.0
// @param:number
// @param说明:
// 需转换数值
//---------------------------------------------------------------------------
function displayComma(str) {
str = '' + str;
if ((str.indexOf("."))!= -1){
str1 = str.substring(0,str.indexOf("."));
str2 = str.substring(str.indexOf("."));
}else{
str1 = str;
}
if (str1.length > 3) {
var mod = str1.length % 3;
var output = (mod > 0 ? (str1.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(str1.length / 3); i++) {
if ((mod == 0) && (i == 0))
output += str1.substring(mod+ 3 * i, mod + 3 * i + 3);
else
output += ',' + str1.substring(mod + 3 * i, mod + 3 * i + 3);
}
if ((str.indexOf("."))!= -1){
output = output + str2;
}
return (output);
}
else return str;
}

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧