<script> function comma(v){ var re=/(\d+)(\d{3})/,s=v.toString(); while(re.test(s))s=s.replace(re,"$1,$2"); return s; } alert(comma(12345678.123)); </script>
fason: 不行啊,效果如下<html> <head> <script> function comma(v){ var re=/(\d+)(\d{3})/,s=v.toString(); while(re.test(s))s=s.replace(re,"$1,$2"); return s; } function show(o){ o.value=comma(o.value); } </script> </head> <body> <input type="text" name="txt" onkeyup="show(txt)"> </dody> </html> </html> 离完美还差两步
fason: 不行啊,效果如下<html> <head> <script> function comma(v){ var re=/(\d+)(\d{3})/,s=v.toString(); while(re.test(s))s=s.replace(re,"$1,$2"); return s; } function show(o){ o.value=comma(o.value); } </script> </head> <body> <input type="text" name="txt" onkeyup="show(txt)"> </dody> </html> </html> 离完美还差两步
<html> <body> <script> function f(v) { var re=/(\d+)(\d{3})/,s=v.toString(); while(re.test(s))s=s.replace(re,"$1,$2"); return s; }var str=100000000.035; str=str.toFixed(2); var res=str.substr(str.indexOf(".")+1,2); str=str.substr(0,str.length-3); str=f(str); str=str+"."+res; alert(str); </script> </body> </html>
最简单的方法就是:var n = 100000000.035; execScript('n = FormatNumber("'+ n +'")','vbscript'); alert(n);不过要IE5.0+
<script> var n = 100000000.035; alert(n.toLocaleString()); </script>
<script> var num = "123456789.123";function c(tmp) { var parts = tmp.split("."); var intPart = parts[0]; var dIntPart = (intPart.split("")).reverse(); var dIntString = dIntPart.join(""); var resultPart = dIntString.replace(/(.{3})(?=[^$])/g,"$1,"); return resultPart.split("").reverse().join("")+"."+parts[1]; }alert(c(num)); </script>
用Number对象的toLocaleString方法会对小数位舍入,保留两位。
<html> <head> <script> function comma(v){ v = v.replace(/,/g,""); var ar = v.split("."); var s = ar[0]; var d = ""; var re=/(\d+)(\d{3})/; while(re.test(s)) s=s.replace(re,"$1,$2"); if(ar.length>1) return s+"."+ar[1]; return s; } function show(o){ o.value=comma(o.value); } </script> </head> <body> <input type="text" name="txt" onkeyup="show(txt)"> </dody> </html> </html>
function comma(v){
var re=/(\d+)(\d{3})/,s=v.toString();
while(re.test(s))s=s.replace(re,"$1,$2");
return s;
}
alert(comma(12345678.123));
</script>
不行啊,效果如下<html>
<head>
<script>
function comma(v){
var re=/(\d+)(\d{3})/,s=v.toString();
while(re.test(s))s=s.replace(re,"$1,$2");
return s;
}
function show(o){
o.value=comma(o.value);
}
</script>
</head>
<body>
<input type="text" name="txt" onkeyup="show(txt)">
</dody>
</html>
</html>
离完美还差两步
不行啊,效果如下<html>
<head>
<script>
function comma(v){
var re=/(\d+)(\d{3})/,s=v.toString();
while(re.test(s))s=s.replace(re,"$1,$2");
return s;
}
function show(o){
o.value=comma(o.value);
}
</script>
</head>
<body>
<input type="text" name="txt" onkeyup="show(txt)">
</dody>
</html>
</html>
离完美还差两步
<body>
<script>
function f(v)
{
var re=/(\d+)(\d{3})/,s=v.toString();
while(re.test(s))s=s.replace(re,"$1,$2");
return s;
}var str=100000000.035;
str=str.toFixed(2);
var res=str.substr(str.indexOf(".")+1,2);
str=str.substr(0,str.length-3);
str=f(str);
str=str+"."+res;
alert(str);
</script>
</body>
</html>
execScript('n = FormatNumber("'+ n +'")','vbscript');
alert(n);不过要IE5.0+
var n = 100000000.035;
alert(n.toLocaleString());
</script>
var num = "123456789.123";function c(tmp)
{
var parts = tmp.split("."); var intPart = parts[0];
var dIntPart = (intPart.split("")).reverse();
var dIntString = dIntPart.join("");
var resultPart = dIntString.replace(/(.{3})(?=[^$])/g,"$1,");
return resultPart.split("").reverse().join("")+"."+parts[1];
}alert(c(num));
</script>
<head>
<script>
function comma(v){
v = v.replace(/,/g,"");
var ar = v.split(".");
var s = ar[0];
var d = "";
var re=/(\d+)(\d{3})/;
while(re.test(s))
s=s.replace(re,"$1,$2");
if(ar.length>1)
return s+"."+ar[1];
return s;
}
function show(o){
o.value=comma(o.value);
}
</script>
</head>
<body>
<input type="text" name="txt" onkeyup="show(txt)">
</dody>
</html>
</html>
在JAVA里怎么实现去逗号、加逗号函数?