我正在使用d3来显示数据,我使用此功能将数字从阿拉伯数字转换为印度数字(1> 1)
function arNum(numb){
var str = numb.toString();
var arabic = {"0" : '٠', "1": '١',"2": '٢',"3": '٣',"4": '٤',"5": '٥',"6": '٦',"7": '٧',"8": '٨',"9": '٩'};
var chars = str.split("");
var newnum = new Array();
for (var i = 0; i < chars.length; i++) {
newnum[i] = arabic[ chars[i] ];
}
return newnum.join("");
}
并且它的工作非常好然后我想添加逗号来分隔1000到1000之间的数字,但我需要使用像1000这样的印度数字来完成它
所以我使用了这个功能,它正在使用阿拉伯数字123456789但不是印度数字:
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return val;
}
所以请帮忙!
答案 0 :(得分:0)
您可以先将逗号添加到阿拉伯数字,然后转换为印度数字。
我在你的JSfiddle http://jsfiddle.net/samehsubscription/F9zCK/中尝试了它并且它工作正常。更新arNum函数如下:
function arNum(numb){
var str = numb.toString();
while (/(\d+)(\d{3})/.test(str.toString())){
str = str.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
var arabic = {"0" : '٠', "1": '١',"2": '٢',"3": '٣',"4": '٤',"5": '٥',"6": '٦',"7": '٧',"8": '٨',"9": '٩'};
var chars = str.split("");
var newnum = new Array();
for (var i = 0; i < chars.length; i++) {
if(chars[i] == ",") newnum[i] = ",";
else
newnum[i] = arabic[ chars[i] ];
}
return newnum.join("");
}
答案 1 :(得分:0)
您可以根据您的要求使用此功能或此功能的ragex:
Number.prototype.NumberFormat = function (decimals, decPoint, thousandsSep) {
decPoint = typeof decPoint !== 'undefined' ? decPoint : '.';
thousandsSep = typeof thousandsSep !== 'undefined' ? thousandsSep : ',';
var parts = this.toFixed(decimals).toString().split('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSep);
return parts.join(decPoint);
};
Just call
val = val.toString().NumberFormat(2);