js 格式化金额,支持选择小数点后位数

getNumberResult中有两个参数,第一个参数是原始数据(可以是number、string),第二个参数是小数点后位数(默认为2);以下提供两种写法:

1、ES6以上可用:

const getNumberResult = (num, decimal = 2) => {
  if (!num) return '';
  num = num[num.length - 1] === '.' && num.match(/\./g).length === 1 ? num : parseFloat(num) //isNaN(parseFloat(num)) ? '' : parseFloat(num).toString();
  // num = typeof num != 'String' ? num.toString() : num;
  if(isNaN(num)) return'';

  num = num.toString();
  // 初次阻止0-9及.以外的字符
  if (!/^[0-9,.]*$/.test(num)) return num.slice(0, -1)

  const reg = /^(([1-9][0-9]*\.[0-9][0-9]?)|([0]\.[0-9][0-9]?)|([1-9][0-9]*)|([0]{1}))$/;
  if (!reg.test(num) && num.match(/\./g).length > 1) {
    num = num.slice(0, -1)
  } else if (num.includes('.') && num.match(/\./g).length > 1) {
    num = num.slice(0, -1)
  } else if (num.includes('.') && !parseInt(decimal)) {
    num = num.slice(0, -1)
  } else if (num.includes('.') && num.split('.')[1].length > decimal) {
    num = num.slice(0, -1)
  }

  if (num.includes('.')) {
    const index = num.indexOf('.');
    const len = num.length;
    return len - 1 - index > decimal ? num.slice(0, index + decimal + 1) : num
  } else {
    return num;
  }
}

2、ES5写法:

function getNumberResult(num, decimal = 2) {
  if (!num) return '';
  num = num[num.length - 1] === '.' && num.match(/\./g).length === 1 ? num : parseFloat(num) //isNaN(parseFloat(num)) ? '' : parseFloat(num).toString();
  // num = typeof num != 'String' ? num.toString() : num;
  if(isNaN(num)) return'';

  num = num.toString();
  // 初次阻止0-9及.以外的字符
  if (!/^[0-9,.]*$/.test(num)) return num.slice(0, -1)

  const reg = /^(([1-9][0-9]*\.[0-9][0-9]?)|([0]\.[0-9][0-9]?)|([1-9][0-9]*)|([0]{1}))$/;
  if (!reg.test(num) && num.match(/\./g).length > 1) {
    num = num.slice(0, -1)
  } else if (num.includes('.') && num.match(/\./g).length > 1) {
    num = num.slice(0, -1)
  } else if (num.includes('.') && !parseInt(decimal)) {
    num = num.slice(0, -1)
  } else if (num.includes('.') && num.split('.')[1].length > decimal) {
    num = num.slice(0, -1)
  }

  if (num.includes('.')) {
    var index = num.indexOf('.');
    var len = num.length;
    return len - 1 - index > decimal ? num.slice(0, index + decimal + 1) : num
  } else {
    return num;
  }
}

可以实时修改其值,以下用vue中的写法举例:

1、模板中写法:

 <el-input v-model="FormData.payAmount" size='mini' @input="changePrice('payAmount')" clearable >
                <template slot="prepend">CNY</template>
              </el-input>

2、js中写法:

data:() => ({
    FormData:{payAmount: 0}
}),
methods: {
     changePrice(key) {
        this.FormData[key] = getNumberResult(this.FormData[key], 2);
      },
}

欢迎留言交流,QQ:1960816818

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天府之绝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值