【发布时间】:2025-11-25 23:20:08
【问题描述】:
我想将数字转换为数字格式。
到目前为止我已经尝试过了:http://plnkr.co/edit/lVJGXyuX0BMvB9QUL5zS?p=preview
function formatMoney(credits) {
console.log(credits+'credits');
var lastThree = credits.substring(credits.length-3);
// var lastThree = credits.slice(-2);
var otherNumbers = credits.substring(0,credits.length-3);
console.log(otherNumbers+'otherNumbers');
if(otherNumbers !== '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
return res;
}
function formatNumber(num) {
var n1, n2;
num = (Math.round(num * 100) / 100) + '' || '';
n1 = num.split('.');
n2 = n1[1] || null;
n1 = n1[0].replace(/(\d)(?=(\d\d)+\d$)/g, ",");
num = n2 ? n1 + '.' + n2 : n1;
return num;
}
我想将数字转换为印度货币。
例子:
1,000
10,000
1,00,000
10,00,000
我使用函数 formatMoney() 得到的输出:1,0,0,000
我使用函数 formatNumber(): 1,000 得到的输出,按 0 后,它变为 NaN0
我在这里做错了什么?
【问题讨论】:
-
这已经有了答案。 *.com/questions/16037165/… 如果这不起作用,我很乐意试一试。
-
-
@Conceptz:我尝试了相同的代码,但对我不起作用。我在这里做错了什么?
-
Emile Paffard-Wray 写了一个很好的答案,请参阅我的评论以了解您需要的调整,如果可行,请为他的答案投票。
标签: javascript angularjs