JS如何將數(shù)字金額轉(zhuǎn)換成中文金額格式
在開發(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)文章
JavaScript DOM實(shí)現(xiàn)簡單留言板
這篇文章主要為大家詳細(xì)介紹了JavaScript DOM實(shí)現(xiàn)簡單留言板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01JS實(shí)現(xiàn)基于Sketch.js模擬成群游動的蝌蚪運(yùn)動動畫效果【附demo源碼下載】
這篇文章主要介紹了JS實(shí)現(xiàn)基于Sketch.js模擬成群游動的蝌蚪運(yùn)動動畫效果,涉及Sketch.js插件的使用及HTML5元素的應(yīng)用技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-08-08JavaScript自定義方法實(shí)現(xiàn)trim()、Ltrim()、Rtrim()的功能
去除字符串兩端的空格,是字符串處理非常常用的方法如何trim() 、Ltrim() 、Rtrim(),可惜的是javascript中無此方法,下面有個(gè)不錯(cuò)的自定義教程感興趣的朋友可以參考下2013-11-11Raphael帶文本標(biāo)簽可拖動的圖形實(shí)現(xiàn)代碼
Javascript和Raphael順便學(xué)習(xí)了一下,主要是為了實(shí)現(xiàn)一個(gè)可拖動的矩形同時(shí)矩形上還得顯示標(biāo)簽,網(wǎng)上關(guān)于這方面的知識提的很是于是本人自不量力寫了一下,感興趣的你可不要錯(cuò)過了哈,希望可以幫助到你2013-02-02原生js實(shí)現(xiàn)隨機(jī)點(diǎn)名
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)隨機(jī)點(diǎn)名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07基于JS實(shí)現(xiàn)table導(dǎo)出Excel并保留樣式
這篇文章主要介紹了基于JS實(shí)現(xiàn)table導(dǎo)出Excel并保留樣式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05