翼度科技»论坛 编程开发 .net 查看内容

js将数字金额转换成中文金额格式

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
在开发中我们经常会遇到处理数字的问题,下面介绍一种处理数字金额转换为中文金额的方式:
我们通常使用三种书面数字系统:全球使用的阿拉伯数字系统和两种本地数字系统(繁体、简体)。常规时我们使用阿拉伯数字(1,2,3等),但在某些情况中,如金融中我们会使用繁体汉字来书写数字,繁体字优点是安全且无法篡改,弥补了阿拉数字易于修改的问题,如在银行帐户存储或支票上使用繁体大写数字。 
书写中文数字时只须输入阿拉伯数字。点击转换即可以实现阿拉伯数字转中文大写,支持元、角、分。 
中文大写书写时的注意事项:

中文大写金额数字应用正楷或行书填写,使用繁体字。如壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整(正)等字样。
一、中文大写金额数字到"元"为止的,在"元"之后,应写"整"(或"正")字,在"角"之后,可以不写"整"(或"正")字。
二、中文大写金额数字前应标明"人民币"字样,大写金额数字有"分"的,"分"后面不写"整"(或"正")字。
三、大写金额数字应紧接"人民币"字样填写,不得留有空白。大写金额数字前未印"人民币"字样的,应加填"人民币"三字。在票据和结算凭证大写金额栏内不得预印固定的"仟、佰、拾、万、仟、佰、拾、元、角、分"字样。
四、阿拉伯数字小写金额数字中有"0"时,中文大写应按照汉语语言规律、金额数字构成和防止涂改的要求进行书写。
以下是具体代码实现:
  1. /**
  2. * convertCurrencyToChinese - 数字转成汉字
  3. * @params num === 要转换的数字
  4. * @return 汉字
  5. * <br>*/<br>function convertCurrencyToChinese(num) {
  6.   if(!num){
  7.     return '零';
  8.   }
  9.   // Constants:
  10.   const MAXIMUM_NUMBER = 99999999999.99;
  11.   // Predefine the radix characters and currency symbols for output:
  12.   const CN_ZERO = "零";
  13.   const CN_ONE = "壹";
  14.   const CN_TWO = "贰";
  15.   const CN_THREE = "叁";
  16.   const CN_FOUR = "肆";
  17.   const CN_FIVE = "伍";
  18.   const CN_SIX = "陆";
  19.   const CN_SEVEN = "柒";
  20.   const CN_EIGHT = "捌";
  21.   const CN_NINE = "玖";
  22.   const CN_TEN = "拾";
  23.   const CN_HUNDRED = "佰";
  24.   const CN_THOUSAND = "仟";
  25.   const CN_TEN_THOUSAND = "万";
  26.   const CN_HUNDRED_MILLION = "亿";
  27.   // const CN_SYMBOL = "人民币";
  28.   const CN_DOLLAR = "元";
  29.   const CN_TEN_CENT = "角";
  30.   const CN_CENT = "分";
  31.   const CN_INTEGER = "整";
  32.   // Variables:
  33.   // let integral; // Represent integral part of digit number.
  34.   // let decimal; // Represent decimal part of digit number.
  35.   let outputCharacters; // The output result.
  36.   // let parts;
  37.   // let digits;
  38.   // let radices;
  39.   // let bigRadices;
  40.   // let decimals;
  41.   let zeroCount;
  42.   let i;
  43.   let p;
  44.   let d;
  45.   let quotient;
  46.   let modulus;
  47.   let currencyDigits = num;
  48.   // Validate input string:
  49.   currencyDigits = currencyDigits.toString();
  50.   if (currencyDigits === "") {
  51.     // alert("Empty input!");
  52.     return "";
  53.   }
  54.   if (currencyDigits.match(/[^,.\d]/) != null) {
  55.     // alert("Invalid characters in the input string!");
  56.     return "";
  57.   }
  58.   if ((currencyDigits).match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null) {
  59.     // alert("Illegal format of digit number!");
  60.     return "";
  61.   }
  62.   // Normalize the format of input digits:
  63.   currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters.
  64.   currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning.
  65.   // Assert the number is not greater than the maximum number.
  66.   if (Number(currencyDigits) > MAXIMUM_NUMBER) {
  67.     // eslint-disable-next-line no-console
  68.     console.warn("输入的金额太大,请重新输入!");
  69.     return "";
  70.   }
  71.   // Process the coversion from currency digits to characters:
  72.   // Separate integral and decimal parts before processing coversion:
  73.   const parts = currencyDigits.split(".");
  74.   // eslint-disable-next-line prefer-const
  75.   let [integral, decimal = ''] = parts;
  76.   if (parts.length > 1) {
  77.     // Cut down redundant decimal digits that are after the second.
  78.     decimal = decimal.substr(0, 2);
  79.   }
  80.   // Prepare the characters corresponding to the digits:
  81.   const digits = [CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE];
  82.   const radices = ["", CN_TEN, CN_HUNDRED, CN_THOUSAND];
  83.   const bigRadices = ["", CN_TEN_THOUSAND, CN_HUNDRED_MILLION];
  84.   const decimals = [CN_TEN_CENT, CN_CENT];
  85.   // Start processing:
  86.   outputCharacters = "";
  87.   // Process integral part if it is larger than 0:
  88.   if (Number(integral) > 0) {
  89.     zeroCount = 0;
  90.     for (i = 0; i < integral.length; i++) {
  91.       p = integral.length - i - 1;
  92.       d = integral.substr(i, 1);
  93.       quotient = p / 4;
  94.       modulus = p % 4;
  95.       if (d === "0") {
  96.         zeroCount++;
  97.       }
  98.       else {
  99.         if (zeroCount > 0) {
  100.           outputCharacters += digits[0];
  101.         }
  102.         zeroCount = 0;
  103.         outputCharacters += digits[Number(d)] + radices[modulus];
  104.       }
  105.       if (modulus === 0 && zeroCount < 4) {
  106.         outputCharacters += bigRadices[quotient];
  107.       }
  108.     }
  109.     outputCharacters += CN_DOLLAR;
  110.   }
  111.   // Process decimal part if there is:
  112.   if (decimal !== "") {
  113.     for (i = 0; i < decimal.length; i++) {
  114.       d = decimal.substr(i, 1);
  115.       if (d !== "0") {
  116.         outputCharacters += digits[Number(d)] + decimals[i];
  117.       }
  118.     }
  119.   }
  120.   // Confirm and return the final output string:
  121.   if (outputCharacters === "") {
  122.     outputCharacters = CN_ZERO + CN_DOLLAR;
  123.   }
  124.   if (decimal === "") {
  125.     outputCharacters += CN_INTEGER;
  126.   }
  127.   return outputCharacters || '';
  128. }
复制代码
以上就是实现过程。

来源:https://www.cnblogs.com/abc-x/p/17582785.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具