本文主要是介绍千分位 转化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// 千分位 转化function toThousands(num,isInt) {
var after;
var before;
var isOne = num % 1;
// 判断是否有 负号
var str = num.toString();
var index = str.indexOf("-");
var numBefore = "";
if (index == -1) {// 没有 -(负号)
numBefore = ""
} else{
numBefore = "-"
}
num = num.toString().replace("-","");
if (isOne == 0) {// 是整数
if (isInt) {
after = "";
} else{
after = ".00";
}
before = parseInt(num);
} else {
after = "." + num.toString().replace(/\d+\.(\d*)/,"$1").replace("-","");
var before_str = num.toString();
before = parseInt(before_str.substring(0,before_str.indexOf('.')));
}
before = (before || 0).toString(), result = '';
while (before.length > 3) {
result = ',' + before.slice(-3) + result;
before = before.slice(0, before.length - 3);
}
if (before) { result = before + result; }
return numBefore + result + after;
}
这篇关于千分位 转化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!