js保留小数(不四舍五入)

function formatDecimal(num, decimal) {
          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)
      }

  

console.log(formatDecimal(3.1415926,6))      --->   3.141592
posted @ 2021-12-02 11:13  54墨玉  阅读(599)  评论(0编辑  收藏  举报