ethers 处理余额显示

 let b = utils.formatUnits(item[0].toString(), 9);
        let str1 = "";
        let str2 = "";
        // 处理有小数点的情况
        if (String(b).indexOf(".") > -1) {
          console.log("是小数");
          // 截取小数点后面的数字
          str1 = b.toString().replace(/\d+\.(\d*)/, "$1");
          // 截取小数点前面的数字
          str2 = b.substring(0, b.indexOf("."));
          let str3 = toThousands(str2);
          b = str3 + "." + str1;
        } else {
          // 处理没有小数点的情况
          b = toThousands(b);
        }
 
 
 
const toThousands = (num) => {
  let result = "",
    counter = 0;
  num = (num || 0).toString();
  for (let i = num.length - 1; i >= 0; i--) {
    counter++;
    result = num.charAt(i) + result;
    if (!(counter % 3) && i != 0) {
      result = "," + result;
    }
  }
  return result;
};
posted @ 2023-05-12 16:14  起风了1573  阅读(57)  评论(0)    收藏  举报