JavaScript实现数字金额转中文大写格式

作者:小满时间:2026-10-30 22:12:06浏览:351

本文介绍在JavaScript中将阿拉伯数字金额转换为中文大写金额的完整方案。涵盖金融场景下的书写规范,包括元角分处理、零的用法及整字规则,并提供可直接使用的转换函数代码,确保数据安全性与合规性。

在Web开发过程中,经常需要处理各类数值数据。本文将介绍一种将普通数字金额转换为中文大写金额的具体方法。

我们通常使用三种书面数字系统:全球使用的阿拉伯数字系统和两种本地数字系统(繁体、简体)。常规时我们使用阿拉伯数字(1,2,3等),但在某些情况中,如金融中我们会使用繁体汉字来书写数字,繁体字优点是安全且无法篡改,弥补了阿拉数字易于修改的问题,如在银行帐户存储或支票上使用繁体大写数字。

用户只需输入阿拉伯数字,系统即可自动转换为中文大写格式,支持元、角、分单位的完整转换。

中文大写金额的书写需遵循特定规范:

金额数字应使用正楷或行书字体填写,并采用繁体字。标准用字包括:壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整(正)等。

当金额写到“元”位时,其后必须写“整”(或“正”)字;若写到“角”位,可以不写“整”(或“正”)字。

中文大写金额数字前必须标明“人民币”字样。若金额包含“分”,则“分”后不写“整”(或“正”)字。

大写金额数字应紧接“人民币”字样填写,中间不得留有空隙。若票据或结算凭证未预印“人民币”字样,需手动加填“人民币”三字。在票据和结算凭证的大写金额栏内,不得预印固定的“仟、佰、拾、万、仟、佰、拾、元、角、分”字样。

当阿拉伯小写金额中含有“0”时,中文大写的书写需遵循汉语语言规律、金额数字构成规则以及防止涂改的要求。

以下是具体的JavaScript代码实现:

/**
 * convertCurrencyToChinese - 数字转成汉字
 * @params num === 要转换的数字
 * @return 汉字
 * 
*/
function convertCurrencyToChinese(num) {
  if(!num){
    return '零';
  }
  // Constants:
  const MAXIMUM_NUMBER = 99999999999.99;
  // Predefine the radix characters and currency symbols for output:
  const CN_ZERO = "零";
  const CN_ONE = "壹";
  const CN_TWO = "贰";
  const CN_THREE = "叁";
  const CN_FOUR = "肆";
  const CN_FIVE = "伍";
  const CN_SIX = "陆";
  const CN_SEVEN = "柒";
  const CN_EIGHT = "捌";
  const CN_NINE = "玖";
  const CN_TEN = "拾";
  const CN_HUNDRED = "佰";
  const CN_THOUSAND = "仟";
  const CN_TEN_THOUSAND = "万";
  const CN_HUNDRED_MILLION = "亿";
  // const CN_SYMBOL = "人民币";
  const CN_DOLLAR = "元";
  const CN_TEN_CENT = "角";
  const CN_CENT = "分";
  const CN_INTEGER = "整";
  // Variables:
  // let integral; // Represent integral part of digit number.
  // let decimal; // Represent decimal part of digit number.
  let outputCharacters; // The output result.
  // let parts;
  // let digits;
  // let radices;
  // let bigRadices;
  // let decimals;
  let zeroCount;
  let i;
  let p;
  let d;
  let quotient;
  let modulus;
  let currencyDigits = num;
  // Validate input string:
  currencyDigits = currencyDigits.toString();
  if (currencyDigits === "") {
    // alert("Empty input!");
    return "";
  }
  if (currencyDigits.match(/[^,.\d]/) != null) {
    // alert("Invalid characters in the input string!");
    return "";
  }
  if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) {
    // alert("Illegal format of digit number!");
    return "";
  }
  // Normalize the format of input digits:
  currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters.
  currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning.
  // Assert the number is not greater than the maximum number.
  if (Number(currencyDigits) > MAXIMUM_NUMBER) {
    // eslint-disable-next-line no-console
    console.warn("输入的金额太大,请重新输入!");
    return "";
  }
  // Process the coversion from currency digits to characters:
  // Separate integral and decimal parts before processing coversion:
  const parts = currencyDigits.split(".");
  // eslint-disable-next-line prefer-const
  let [integral, decimal = ''] = parts;
  if (parts.length > 1) {
    // Cut down redundant decimal digits that are after the second.
    decimal = decimal.substr(0, 2);
  }
  // Prepare the characters corresponding to the digits:
  const digits = [CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE];
  const radices = ["", CN_TEN, CN_HUNDRED, CN_THOUSAND];
  const bigRadices = ["", CN_TEN_THOUSAND, CN_HUNDRED_MILLION];
  const decimals = [CN_TEN_CENT, CN_CENT];
  // Start processing:
  outputCharacters = "";
  // Process integral part if it is larger than 0:
  if (Number(integral) > 0) {
    zeroCount = 0;
    for (i = 0; i < integral.length; i++) {
      p = integral.length - i - 1;
      d = integral.substr(i, 1);
      quotient = p / 4;
      modulus = p % 4;
      if (d === "0") {
        zeroCount++;
      }
      else {
        if (zeroCount > 0) {
          outputCharacters += digits[0];
        }
        zeroCount = 0;
        outputCharacters += digits[Number(d)] + radices[modulus];
      }
      if (modulus === 0 && zeroCount < 4) {
        outputCharacters += bigRadices[quotient];
      }
    }
    outputCharacters += CN_DOLLAR;
  }
  // Process decimal part if there is:
  if (decimal !== "") {
    for (i = 0; i < decimal.length; i++) {
      d = decimal.substr(i, 1);
      if (d !== "0") {
        outputCharacters += digits[Number(d)] + decimals[i];
      }
    }
  }
  // Confirm and return the final output string:
  if (outputCharacters === "") {
    outputCharacters = CN_ZERO + CN_DOLLAR;
  }
  if (decimal === "") {
    outputCharacters += CN_INTEGER;
  }
  return outputCharacters || '';
}

上述即为完整的转换流程与逻辑。

本文介绍了JS将数字金额转换为中文大写格式的方法。如需了解更多相关JS数字转换内容,可查阅本站往期文章或继续浏览后续内容,感谢支持。

作者最新文章