欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS如何將數(shù)字金額轉(zhuǎn)換成中文金額格式

 更新時(shí)間:2023年07月27日 09:40:55   作者:消逝的風(fēng)i  
這篇文章主要介紹了JS如何將數(shù)字金額轉(zhuǎn)換成中文金額格式,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在開發(fā)中我們經(jīng)常會遇到處理數(shù)字的問題,下面介紹一種處理數(shù)字金額轉(zhuǎn)換為中文金額的方式:

我們通常使用三種書面數(shù)字系統(tǒng):全球使用的阿拉伯?dāng)?shù)字系統(tǒng)和兩種本地?cái)?shù)字系統(tǒng)(繁體、簡體)。常規(guī)時(shí)我們使用阿拉伯?dāng)?shù)字(1,2,3等),但在某些情況中,如金融中我們會使用繁體漢字來書寫數(shù)字,繁體字優(yōu)點(diǎn)是安全且無法篡改,彌補(bǔ)了阿拉數(shù)字易于修改的問題,如在銀行帳戶存儲或支票上使用繁體大寫數(shù)字。 

書寫中文數(shù)字時(shí)只須輸入阿拉伯?dāng)?shù)字。點(diǎn)擊轉(zhuǎn)換即可以實(shí)現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)中文大寫,支持元、角、分。 

中文大寫書寫時(shí)的注意事項(xiàng):

中文大寫金額數(shù)字應(yīng)用正楷或行書填寫,使用繁體字。如壹、貳、叁、肆、伍、陸、柒、捌、玖、拾、佰、仟、萬、億、元、角、分、零、整(正)等字樣。

一、中文大寫金額數(shù)字到"元"為止的,在"元"之后,應(yīng)寫"整"(或"正")字,在"角"之后,可以不寫"整"(或"正")字。

二、中文大寫金額數(shù)字前應(yīng)標(biāo)明"人民幣"字樣,大寫金額數(shù)字有"分"的,"分"后面不寫"整"(或"正")字。

三、大寫金額數(shù)字應(yīng)緊接"人民幣"字樣填寫,不得留有空白。大寫金額數(shù)字前未印"人民幣"字樣的,應(yīng)加填"人民幣"三字。在票據(jù)和結(jié)算憑證大寫金額欄內(nèi)不得預(yù)印固定的"仟、佰、拾、萬、仟、佰、拾、元、角、分"字樣。

四、阿拉伯?dāng)?shù)字小寫金額數(shù)字中有"0"時(shí),中文大寫應(yīng)按照漢語語言規(guī)律、金額數(shù)字構(gòu)成和防止涂改的要求進(jìn)行書寫。

以下是具體代碼實(shí)現(xiàn):

/**
 * convertCurrencyToChinese - 數(shù)字轉(zhuǎn)成漢字
 * @params num === 要轉(zhuǎn)換的數(shù)字
 * @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 || '';
}

以上就是實(shí)現(xiàn)過程。

到此這篇關(guān)于JS如何將數(shù)字金額轉(zhuǎn)換成中文金額格式的文章就介紹到這了,更多相關(guān)js數(shù)字金額轉(zhuǎn)換中文金額內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論