数据保留两位小数点,不四舍五入
一般我们处理小数点都是toFixed,这个四舍五入的。如果不想死五入可以先转转化为字符串然后截取在用toFixed
formatDecimal(num, decimal) {
if(num){
num = num.toString()
let index = num.indexOf('.')
if (index !== -1) {
num = num.substring(0, decimal + index + 1)
} else {
num = num.substring(0)
}
}
return parseFloat(num).toFixed(decimal)
}
let newDiscountPrice = this.formatDecimal(discountPrice, 2)
newDiscountPrice = parseFloat(newDiscountPrice)*100/10 //有小数点的的时候最好转成整数在处理否则会呕溢出情况
//newDiscountPrice = parseFloat(newDiscountPrice)*10 //像是这种小数点直接乘以10就会出现溢出情况
// let newDiscountPrice = 5
newDiscountPrice = newDiscountPrice%1==0 ? newDiscountPrice+='.0' : newDiscountPrice;//一般整数5后面要加5.0,任何整数都能被自身整除也就是余数是0