/**
* 将金额使用千分号格式化
* @param {金额} num
*/
export function formatNumberRgx(num) {
if (!num) {
return;
}
var temp = num;
var parts = parseFloat(temp).toString().split('.');
var flag = temp.indexOf(',') === -1;
if (flag) {
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
if (parts.length === 1) { // 补足两位小数
parts[1] = '00'
} else {
parts[1].length <= 1 ? parts[1] = parts[1] + '0' : parts[1] = parts[1];
}
return parts.join('.');
} else {
return num
}
}
/**
* 将千分号去掉
* @param {金额} num
*/
export function formatMoneyRgx(num) {
if (!num) {
return;
}
var temp = num;
if (typeof temp === 'number') {
temp = temp.toString()
}
var parts = temp.split('.');
var moneys = parts[0].split(',');
var len = moneys.length;
if (len > 1) {
if (parts.length === 1) { // 补足两位小数
parts[1] = '00'
} else {
parts[1].length <= 1 ? parts[1] = parts[1] + '0' : parts[1] = parts[1];
}
return moneys.join('') + '.' + parts[1];
} else {
return num
}
}