首页 > 代码库 > js 金额格式化
js 金额格式化
//格式化金额,小数点几位
function fmoney(money,digit){
if(null == money ){
return "";
}
money = money +"";
var array = money.split(".");
var s =array[0];
var r="";
var m= s.substring(0,1);
if(‘-‘== m){
s = s.substring(1);
}else {
m ="";
}
var f= s.split("").reverse();
for(var i = 0;i< f.length;i++){
r += f[i] + ((i + 1) % 3 == 0 && (i + 1) != f.length ? "," : "");
}
var after = "";
if(array.length>1){
after = array[1];
if(after.length>digit){
after =after.substring(0,digit);
}
}
return m + r.split("").reverse().join("") + (array.length>1?"."+ after:"");
}
isString: function(str){
return (typeof str==‘string‘)&&str.constructor==String;
},
// 千分位显示数据
formatThousandths: function(num, decimal){
if($.isString(num)) num = parseFloat(num);
if(null == decimal || decimal == undefined) decimal = 2;
return (num.toFixed(decimal) + ‘‘).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, ‘$&,‘);
}
js 金额格式化