vhbjknkm 2022-03-01 15:29 采纳率: 100%
浏览 81
已结题

前端要怎么封装一个公共方法,把之前币种的公共方法替换掉

我想要达到的结果

这个在前端要怎么改
46.017700
将后面的0给去掉
要是
46.017717
就不用去掉数字

如果是六个0就保留小数点后两位
0.000000
改成
0.00

增加币种公共方法
这个是要替换掉的,有没有什么方法达到我要的结果
  common.currencyFormat = function (value, currencyId, type, enterpriseId) {
    return mf.currencyFormat(value, currencyId, type, enterpriseId)
  }
  common.currencyFormat = function (value, currencyId, type, enterpriseId) {
    var num = mf.currencyFormat(value, currencyId, type, enterpriseId)
    num = num.toString().split(".");  // 分隔小数点
    var arr = num[0].split("").reverse();  // 转换成字符数组并且倒序排列
    var res = [];
    for (var i = 0, len = arr.length; i < len; i++) {
      if (i % 3 === 0 && i !== 0) {
        res.push(",");   // 添加分隔符
      }
      res.push(arr[i]);
    }
    res.reverse(); // 再次倒序成为正确的顺序
    if (num[1]) {  // 如果有小数的话添加小数部分
      res = res.join("").concat("." + num[1]);
    } else {
      res = res.join("");
    }
    return res
  }

  • 写回答

4条回答 默认 最新

  • 在下月亮有何贵干 前端领域优质创作者 2022-03-01 16:04
    关注
        const check = (num) => {
            let res = Number(num)
            if (!String(res).includes('.')) {
                return res.toFixed(2)
            } else if (String(res).split('.')[1].length < 2) {
                return res.toFixed(2)
            } else {
                return res
            }
        }
    
        console.log(check(46.017700))//46.0177
        console.log(check(46.000000))//46.00
        console.log(check(46.017717))//46.017717
        console.log(check(46.01))//46.01
        console.log(check(46.1))//46.10
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月5日
  • 已采纳回答 3月2日
  • 创建了问题 3月1日

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录