保留小数点后N位方法

四舍五入保留小数后 n 位

补0: 使用 toFixed(digits) 方法

  • 四舍五入为指定的小数位数
  • 返回字符串
  • 小数实际位数不够指定的位数,不足的部分会补 0
(1).toFixed(2) // '1.00'
(1.2).toFixed(2) // '1.20'
(-2).toFixed(2) // '-2.00'
(1.12345678).toFixed(7) // '1.1234568'

劣势

  • 参数必须是 0 ~ 20 之间的值,包括 0 和 20。参数 < 0 报错。
  • 会出现数字精度问题。详见ECMAScript 中的 toFixed 定义。
(1111111111.1234567891234).toFixed(8) // '1111111111.12345672'

不补0:自定义转换方法

  • 四舍五入为指定的小数位数
  • 返回数字
  • 小数实际位数不够指定的位数,不足的部分不会补 0
function roundFun(number, precision) {
  return Math.round(+number + 'e' + precision) / Math.pow(10, precision);
  }
  // 或
  function roundFun(number, precision) {
    return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision);
    }
roundFun(1, 2) // 1
    roundFun(1.2, 2) // 1.2
    roundFun(-2, 2) // -2
    roundFun(1.12345678, 7) // 1.1234568
    roundFun(1111111111.1234567891234, 8) // 1111111111.1234567

精确显示小数后 n 位

不补0

  • 精确保留小数后 n 位
  • 返回数字
  • 小数实际位数不够指定的位数,不足的部分不会补 0
const keepDecimal = (numStr, precision) => {
      const regexp = new RegExp(`^(\\-|\\+)?\\d+(?:\\.\\d{0,${precision}})?`)
        const result = numStr.match(regexp)
          return result ? result[0] : ''
          }
keepDecimal('1', 2) // '1'
          keepDecimal('1.2', 2) // '1.2'
          keepDecimal('-2', 2) // '-2'
          keepDecimal('1.12345678', 7) // '1.1234567'
          keepDecimal('12345.1234567891234', 8) // '12345.12345678'
          keepDecimal('123456789.1234567891234', 10) // '123456789.1234567891'
          keepDecimal('abcd', 10) // ''

劣势

  • 数字开头的字符串支持转换
keepDecimal('122sss', 10) // '122'

补0

  • 精确保留小数后 n 位
  • 返回字符串
  • 小数实际位数不够指定的位数,不足的部分会补 0
const keepDecimal = (numStr, precision) => {
            const regexp = new RegExp(`^(\\-|\\+)?\\d+(?:\\.\\d{0,${precision}})?`)
              const result = numStr.match(regexp)
                return result ? result[0] : ''
                }

            const keepDecimalPlaces = (numStr, precision) =&gt; {
              let numberStr = keepDecimal(numStr, precision)
                if (!numberStr) return ''
                
                  let rs = numberStr.indexOf('.')
                    if (rs &lt; 0 &amp;&amp; precision &gt; 0) {
                        rs = numberStr.length
                            numberStr += '.'
                              }
                                while (numberStr.length &lt;= rs + precision) {
                                    numberStr += '0'
                                      }
                                        return numberStr
                                        }</code></pre><pre><code class="js">keepDecimalPlaces('1', 2) // '1.00'
                                        keepDecimalPlaces('1.2', 2) // '1.20'
                                        keepDecimalPlaces('-2', 2) // '-2.00'
                                        keepDecimalPlaces('1.12345678', 7) // '1.1234567'
                                        keepDecimalPlaces('12345.1234567891234', 8) // '12345.12345678'
                                        keepDecimalPlaces('123456789.1234567891234', 10) // '123456789.1234567891'
                                        keepDecimalPlaces('123456789.000001', 10) // '123456789.0000010000'
                                        keepDecimalPlaces('abcd', 10) // ''
                                        keepDecimalPlaces('abcd123', 10) // ''</code></pre><p><strong>劣势</strong></p><ul><li>数字开头的字符串支持转换</li></ul><pre><code class="js">keepDecimalPlaces('122sss', 10) // '122.0000000000'</code></pre>