数字小数点往左隔3位加逗号【76884526.88 变 67,884,526.88】

icarrien 2011-05-04 03:02:58
达到效果如下,想必你一看就明白。

//25.88 > 25.88
//2555.88 > 2,555.88
//76884526.88 > 67,884,526.88



String.prototype.format = function(){
var end = this.slice(-3),
start = this.slice(0, -3),
len = Math.ceil(start.length/3),
arr = [];
for(var i=0; i<len ; i++){
arr.push(start.slice(-3));
start = start.slice(0, start.length-3);
}
arr.reverse();
return arr.join() + end;
}

var a = 76884526.88;
a.format() //return 67,884,526.88


请教大家,还有没有更好的解决方案
...全文
299 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lfkcn 2011-05-05
  • 打赏
  • 举报
回复
正则应该是最简单的方案吧
prototyper 2011-05-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chendong152 的回复:]
..........
现提供一个可匹配不带小数点的方法
"2522323.88".replace(/(?=(\d{3})+(\.\d+)?$)/g,",")
不过此方法也有bug,就是当小数位数超过两位的时候,小数位也会被添加逗号。
js好象不支持后顾断言,所以没想到好的办法
[/Quote]

1、“(?!^)”是不能省略的,否则小数点左边数的位数为3位或者任意3的倍数,处理后打头就会添加一个“,”号。例如“255.88”处理后为“,255.88”,“255555.88”处理后为“,255,555.88”。
2、兼容“不带小数点”,JS正则虽不支持后顾断言,还是有办法解决的。等开完会,有时间我再写一个。
chendong152 2011-05-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 prototyper 的回复:]
没那么复杂,就一行正则:
NumberString.replace(/(?!^)(?=(\d{3})+\.)/g, "$&,")

TEST:
alert("25.88" .replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
alert("2555.88" .replace(/(?!^)(?=(\d{3})+\.)/g, "$&,")……
[/Quote]

这位兄弟的方法正确。
现提供一个可匹配不带小数点的方法
"2522323.88".replace(/(?=(\d{3})+(\.\d+)?$)/g,",")
不过此方法也有bug,就是当小数位数超过两位的时候,小数位也会被添加逗号。
js好象不支持后顾断言,所以没想到好的办法
prototyper 2011-05-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cj205 的回复:]

JScript code
String.prototype.format = function(){
return this.replace(/\d(?=(?:\d{3})+\b)/g,function(v) {
return v + ',';
});
}
var a = 76884526.88;
al……
[/Quote]

if
var a = 76884526.8876767;
then
alert(a.toString().format()) ===> 76,884,526.8,876,767 ????
endif
prototyper 2011-05-04
  • 打赏
  • 举报
回复
没那么复杂,就一行正则:
NumberString.replace(/(?!^)(?=(\d{3})+\.)/g, "$&,")

TEST:
alert("25.88" .replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
alert("2555.88" .replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
alert("76884526.88" .replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
alert("768845266.88".replace(/(?!^)(?=(\d{3})+\.)/g, "$&,"));
likeajin 2011-05-04
  • 打赏
  • 举报
回复
-号表示负数
icarrien 2011-05-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 likeajin 的回复:]

function number(v) {
/*
* 如果是小数,想四舍五入就把下一句取消注释
*/
// v = (Math.round((v-0)*100))/100;
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
……
[/Quote]


问一下兄弟。
if(v.charAt(0) == '-'){
return '-' + v.substr(1);
}


检测字符串第一位的字符是否为"-",如果是就进行返回‘-’加1后的所有字符串。
他本来就已经是正确的数了,为什么还要这样的操作呢。


Mr-Jee 2011-05-04
  • 打赏
  • 举报
回复
  	String.prototype.format = function(){
return this.replace(/\d(?=(?:\d{3})+\b)/g,function(v) {
return v + ',';
});
}
var a = 76884526.88;
alert(a.toString().format()); //return 67,884,526.88
cb1156 2011-05-04
  • 打赏
  • 举报
回复
  <script type="text/javascript">
var a=115151342343246.213;
function format(num){
var temp=(""+num).split('.')[0];
var yu=temp.length%3;
var fin=temp.substr(0,yu)+temp.substr(yu).replace(/(\d{3})/g,",$1")+"."+(""+num).split('.')[1];
if(yu==0){
fin=fin.substr(1);
}

alert(fin);
}

format(a);

</script>
likeajin 2011-05-04
  • 打赏
  • 举报
回复
function number(v) {
/*
* 如果是小数,想四舍五入就把下一句取消注释
*/
// v = (Math.round((v-0)*100))/100;
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.'),
whole = ps[0],
sub = ps[1] ? ps[1] : '',
r = /(\d+)(\d{3})/;
/*
* 格式化整数部分
*/
while (r.test(whole)) {
whole = whole.replace(r, '$1' + ',' + '$2');
}
/*
* 格式化小数部分
r=/(\d{3})(\d+)/;
while(r.test(sub)){
sub = sub.replace(r, '$1' + ',' + '$2');
}
*/
v = whole +'.'+ sub;


if(v.charAt(0) == '-'){
return '-' + v.substr(1);
}
return v;
}

87,910

社区成员

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

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