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

我想要达到的结果

这个在前端要怎么改
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
  }

    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

直接用Number转换下,
后面的0就自动去掉了。
如果是0就toFixed(2)就可以了。

img

Number('11.21200')会自动去掉多余得0,至于全是0可以判断,如果全是0就toFixed(2)