zoukankan      html  css  js  c++  java
  • vue格式化数字

    vue格式化数字

    /**
     * @description 格式化金额
     * @param number:要格式化的数字
     * @param decimals:保留几位小数 默认0位
     * @param decPoint:小数点符号 默认.
     * @param thousandsSep:千分位符号 默认为,
     */
    export function formatMoney(number, decimals = 0, decPoint = '.', thousandsSep = ',') {
      number = (number + '').replace(/[^0-9+-Ee.]/g, '')
      const n = !isFinite(+number) ? 0 : +number
      const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
      const sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
      const dec = (typeof decPoint === 'undefined') ? '.' : decPoint
      let s = ''
      const toFixedFix = function(n, prec) {
        const k = Math.pow(10, prec)
        return '' + Math.ceil(n * k) / k
      }
      s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
      const re = /(-?d+)(d{3})/
      while (re.test(s[0])) {
        s[0] = s[0].replace(re, '$1' + sep + '$2')
      }
      if ((s[1] || '').length < prec) {
        s[1] = s[1] || ''
        s[1] += new Array(prec - s[1].length + 1).join('0')
      }
      return s.join(dec)
    }
    
    /**
     * 格式化数字
     * @param number
     * @param decimals
     */
    export function formatDecimals(number, decimals = 2) {
      return formatMoney(number, decimals, '.', '')
    }
  • 相关阅读:
    IIS Admin Service安装
    Linux常用命令总结
    Mysql常用命令操作小结
    mysql常用操作
    初识linux
    python基础
    接口测试基础
    MYSQL笔记
    mysql使用存储函数批量添加数据
    linux的基础命令(博客使用测试中 更新中)
  • 原文地址:https://www.cnblogs.com/yeminglong/p/13032585.html
Copyright © 2011-2022 走看看