var getFloatStr = function(num){
num += '';
num = num.replace(/[^0-9|\.]/g, '');
if(/^0+/)
num = num.replace(/^0+/, '');
if(!/\./.test(num))
num += '.00';
if(/^\./.test(num))
num = '0' + num;
num += '00';
num = num.match(/\d+\.\d{2}/)[0];
console.log(num)
}
getFloatStr('0000000000000000000.1'); //0.10
getFloatStr('fdsfsd');//0.00
getFloatStr('1');//1.00
getFloatStr(1000);//1000.00
js int类型数字后自动添加两位.00,保留小数点后两位,金额格式化
最新推荐文章于 2024-06-04 12:22:22 发布