一般我們處理小數點都是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