人民币数字大写转换工具


function convertCurrency(currencyDigits) {
  let integral, decimal, outputCharacters, parts, digits, radices, bigRadices, decimals, zeroCount, i, p, d, ds, quotient, modulus;
  if (typeof (currencyDigits) === 'number') currencyDigits = String(currencyDigits);
  if ("" == currencyDigits) return alert("不能为空 请输入数字金额!如:123.23"), "";
  if (null != currencyDigits.match(/[^,.\d]/)) return alert("输入字符串中的字符无效!"), "";
  if (null == currencyDigits.match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/)) return alert("请输入正确的数字金额!"), "";
  if (currencyDigits = (currencyDigits = currencyDigits.replace(/,/g, "")).replace(/^0+/, ""), Number(currencyDigits) >
    99999999999.99) return alert("Too large a number to convert!"), "";
  if ((parts = currencyDigits.split(".")).length > 1 ? (integral = parts[0], decimal = (decimal = parts[1]).substr(0,
    2)) : (integral = parts[0], decimal = ""), digits = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌",
      "玖"), radices = new Array("", "拾", "佰", "仟"), bigRadices = new Array("", "万", "亿"), decimals = new Array(
        "角", "分"), outputCharacters = "", Number(integral) > 0) {
    for (zeroCount = 0, i = 0; i < integral.length; i++) quotient = (p = integral.length - i - 1) / 4, modulus = p %
      4, "0" == (d = integral.substr(i, 1)) ? zeroCount++ : (zeroCount > 0 && (outputCharacters += digits[0]),
        zeroCount = 0, outputCharacters += digits[Number(d)] + radices[modulus]), 0 == modulus && zeroCount < 4 &&
      (outputCharacters += bigRadices[quotient]);
    outputCharacters += "元"
  }
  if ("" != decimal)
    for (i = 0; i < decimal.length; i++) d = decimal.substr(i, 1), ds = decimal.substr(-1, 1), outputCharacters +=
      0 == d ? 0 == ds ? "" : digits[Number(d)] : digits[Number(d)] + decimals[i];
  return "" == outputCharacters && (outputCharacters = "零元"), "" == decimal && (outputCharacters += "整"),
    outputCharacters = "" + outputCharacters
}

console.log(convertCurrency('123.23')) // 壹佰贰拾叁元贰角叁分
console.log(convertCurrency(999.99)) // 玖佰玖拾玖元玖角玖分
console.log(convertCurrency(0)) // 零元整

  目录