- 数字类型 / 字符串类型 金额转为标准金额格式(带千分位’,’ 并保留两位小数点)
//格式化金额为2位小数和千分位
Vue.prototype.formatMoney = (str)=>{
if(!str){
return '';
}
var num = str.toString().replace(/,/g,'');
if(!/^[0-9]+\.?[0-9]*$/.test(num)){
return '';
} else {
return (+num || 0).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g,',');
}
}
参考: 同事A
- 标准金额格式转回 float 数字类型
// 金额格式 -> 数字格式
moneyToNumber(str) {
if (!str) {
return 0;
}
if (typeof (str) == 'number') {
return str;
}
let num = parseFloat(str.replace(/[^\d\.-]/g, ""));
// console.log('金额数 =>', num);
return num;
},