首页 > 其他分享 >js 金额转大写

js 金额转大写

时间:2022-12-03 14:57:11浏览次数:59  
标签:10 金额 大写 replace num let Math js unit

参考:https://www.yisu.com/zixun/353425.html

 

// 金额转为大写
export function functiondigitUppercase(price) {
  const fraction = ["角", "分"];
  const digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
  const unit = [
    ["元", "万", "亿"],
    ["", "拾", "佰", "仟"],
  ];
  let num = Math.abs(price);
  let s = "";
  fraction.forEach((item, index) => {
    s += (digit[Math.floor(num * 10 * 10 ** index) % 10] + item).replace(
      /零./,
      ""
    );
  });
  s = s || "整";
  num = Math.floor(num);
  for (let i = 0; i < unit[0].length && num > 0; i += 1) {
    let p = "";
    for (let j = 0; j < unit[1].length && num > 0; j += 1) {
      p = digit[num % 10] + unit[1][j] + p;
      num = Math.floor(num / 10);
    }
    s = p.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i] + s;
  }
  return s
    .replace(/(零.)*零元/, "元")
    .replace(/(零.)+/g, "零")
    .replace(/^整$/, "零元整");
}

 

标签:10,金额,大写,replace,num,let,Math,js,unit
From: https://www.cnblogs.com/jqynr/p/16947660.html

相关文章