數據保留兩位小數點,不四舍五入


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM