在开发中我们经常会遇到处理数字的问题,下面介绍一种处理数字金额转换为中文金额的方式:

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

书写中文数字时只须输入阿拉伯数字。点击转换即可以实现阿拉伯数字转中文大写,支持元、角、分。 

中文大写书写时的注意事项:

中文大写金额数字应用正楷或行书填写,使用繁体字。如壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整(正)等字样。

一、中文大写金额数字到"元"为止的,在"元"之后,应写"整"(或"正")字,在"角"之后,可以不写"整"(或"正")字。

二、中文大写金额数字前应标明"人民币"字样,大写金额数字有"分"的,"分"后面不写"整"(或"正")字。

三、大写金额数字应紧接"人民币"字样填写,不得留有空白。大写金额数字前未印"人民币"字样的,应加填"人民币"三字。在票据和结算凭证大写金额栏内不得预印固定的"仟、佰、拾、万、仟、佰、拾、元、角、分"字样。

四、阿拉伯数字小写金额数字中有"0"时,中文大写应按照汉语语言规律、金额数字构成和防止涂改的要求进行书写。

以下是具体代码实现:

/**
 * 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 || ''; }

以上就是实现过程。

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/abc-x/p/17582785.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程