金额格式化
作者:互联网
1.判断正负 + -号 ,不是数字返回 ‘-’
export const toThousandsNoZero = num => {
return num ? ((isNaN(parseFloat(num.toString().replace(/,/g, ''))) ? 1 : parseFloat(num.toString().replace(/,/g, '')))).toFixed(2).toString().replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
return $1 + ','
}) : '0.00'
}
//没有值返回 0.00
export const ZeroAndNegative= num =>{
if(num!=='' &&!isNaN(num)){
let po=Math.abs(num)
if(num<0){
return '- ¥ '+toThousandsNoZero(po)
}else{
return '¥ '+toThousandsNoZero(po)
}
}else{
return '¥ 0.00'
}
}
//没有值返回'-' 0返回也是'-'
export const ZeroAndNegativeB= num =>{
if(num!=='' &&!isNaN(num)){
let po=Math.abs(num)
if(num<0){
return '- ¥ '+toThousandsNoZero(po)
}else if(num>0){
return '¥ '+toThousandsNoZero(po)
}else{
return '-'
}
}else{
return '-'
}
}
2. numeral:格式化和操作数字的 JavaScript 库。可以通过format方法添加千分符,也可以增加其他符号。
例:{{amountNumeralFormart(homeBalance.balance,'0,0.00')}}
import numeral from 'numeral'
export const amountNumeralFormart = (amount,formater) => {
if (!amount) return '0.00';
let result = ''
try{
let amountNumber = Number(amount)
result = numeral(amountNumber).format(formater);
}catch (e) {
console.log('金额格式化错误:'+amount +",e:"+e)
}
return result;
}
标签:return,0.00,金额,amount,num,let,numeral,格式化 来源: https://blog.csdn.net/weixin_43138050/article/details/120726741