JavaScript 动态将数字金额转化为中文大写金额

代码如下:

function convertCurrency(currencyDigits) {
// Constants:
var MAXIMUM_NUMBER = 99999999999.99;
// Predefine the radix characters and currency symbols for output:
var CN_ZERO = "零";
var CN_ONE = "壹";
var CN_TWO = "贰";
var CN_THREE = "叁";
var CN_FOUR = "肆";
var CN_FIVE = "伍";
var CN_SIX = "陆";
var CN_SEVEN = "柒";
var CN_EIGHT = "捌";
var CN_NINE = "玖";
var CN_TEN = "拾";
var CN_HUNDRED = "佰";
var CN_THOUSAND = "仟";
var CN_TEN_THOUSAND = "万";
var CN_HUNDRED_MILLION = "亿";
var CN_SYMBOL = "人民币";
var CN_DOLLAR = "元";
var CN_TEN_CENT = "角";
var CN_CENT = "分";
var CN_INTEGER = "整";

// Variables:
var integral; // Represent integral part of digit number.
var decimal; // Represent decimal part of digit number.
var outputCharacters; // The output result.
var parts;
var digits, radices, bigRadices, decimals;
var zeroCount;
var i, p, d;
var quotient, modulus;

// 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) {
alert("您输入的金额太大,请重新输入!");
return "";
}

// Process the coversion from currency digits to characters:
// Separate integral and decimal parts before processing coversion:
parts = currencyDigits.split(".");
if (parts.length > 1) {
integral = parts[0];
decimal = parts[1];
// Cut down redundant decimal digits that are after the second.
decimal = decimal.substr(0, 2);
}
else {
integral = parts[0];
decimal = "";
}
// Prepare the characters corresponding to the digits:
digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE);
radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND);
bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION);
decimals = new Array(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;
}
outputCharacters = CN_SYMBOL + outputCharacters;
return outputCharacters;
}

(0)

相关推荐

  • JS 数字转换为大写金额的简单实例

    JS 数字转换为大写金额的简单实例 function DX(n) { if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法"; var unit = "千百拾亿千百拾万千百拾元角分", str = ""; n += "00"; var p = n.indexOf('.'); if (p >= 0) n = n.substring(0, p) + n.substr(p+1,

  • JavaScript 动态将数字金额转化为中文大写金额

    复制代码 代码如下: function convertCurrency(currencyDigits) { // Constants: var MAXIMUM_NUMBER = 99999999999.99; // Predefine the radix characters and currency symbols for output: var CN_ZERO = "零"; var CN_ONE = "壹"; var CN_TWO = "贰"

  • Java将数字金额转为大写中文金额

    本文实例为大家分享了Java将数字金额转为大写中文金额的具体代码,供大家参考,具体内容如下 前言: 输入数字金额参数,运行程序得到其对应的大写中文金额:例如:输入- 12.56,输出- 12.56:壹拾贰元伍角陆分:重点来了:本人亲测有效. 奉上代码: /** *@Title: ConvertUpMoney * @Description: 将数字金额转为大写汉字金额 * @date: 2019年6月18日 下午10:52:27 */ public class ConvertUpMoney { /

  • C#实现金额转换成中文大写金额

    本文实例为大家分享了C#金额转换成中文大写金额的具体代码,供大家参考,具体内容如下 /// <summary> /// 金额转换成中文大写金额 /// </summary> /// <param name="LowerMoney">eg:10.74</param> /// <returns></returns> public static string MoneyToUpper(string LowerMoney)

  • java实现整数转化为中文大写金额的方法

    在日常生活中,我们经常会将阿拉伯数字转化为中文大写的情况:"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "佰", "仟", "万", "亿",因此

  • PHP数字金额转换成中文大写显示

    整个功能其实不难,只是还算挺实用,所以记一下哈,其他编程语言转换一下也是能可以的! 思路:把传过来的金额转换成整数和小数两部分,再对其分别进行转换处理 代码附上: function moneyToString($num) { $digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']; $radices =['', '拾', '佰', '仟', '万', '亿']; $bigRadices = ['', '万', '亿']; $dec

  • php将金额数字转化为中文大写

    php将金额数字转化为中文大写 echo toChineseNumber(1234567890);//壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾圆 function toChineseNumber($money){ $money = round($money,2); $cnynums = array("零","壹","贰","叁","肆","伍","陆","柒&q

  • JavaScript将数字转换成大写中文的方法

    本文实例讲述了JavaScript将数字转换成大写中文的方法.分享给大家供大家参考.具体实现方法如下: function intToChinese ( str ) { str = str+''; var len = str.length-1; var idxs = ['','十','百','千','万','十','百','千','亿','十','百','千','万','十','百','千','亿']; var num = ['零','壹','贰','叁','肆','伍','陆','柒','捌',

  • 解决idea开发遇到javascript动态添加html元素时中文乱码的问题

    起因 一个几个月前做的小网站,这两天翻出来再看看,发现JavaScript文件中动态添加html元素中的中文乱码了,但是从后台获取的动态中文资源没问题,只有js文件中的静态中文资源,加载到网页中显示乱码 不要在意日期中年份部分的错误 过程 在网上看了很多博客,总结到最后就是要把tomcat.jsp.js.servlet的编码格式统一即可,但是我确定我早就将编码统一为utf-8 包括JavaScript文件的编码格式也多次重写为其他编码格式然后再次重写为utf-8,依然不行 结果 多次尝试无果,我

  • 数字金额千位分隔的Javascript

    将数字金额进行千位分隔 //将数字金额进行千位分隔 function formatNum(theObj){ var digit = theObj.value.indexOf("."); // 取得小数点的位置 var int = theObj.value.substr(0,digit); // 取得小数中的整数部分 var i; var mag = new Array(); var word; if (theObj.value.indexOf(".") == -1)

  • JS实现将数字金额转换为大写人民币汉字的方法

    本文实例讲述了JS实现将数字金额转换为大写人民币汉字的方法.分享给大家供大家参考,具体如下: //代码如下所示: function convertCurrency(money) { //汉字的数字 var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'); //基本单位 var cnIntRadice = new Array('', '拾', '佰', '仟'); //对应整数部分扩展单位 var cnInt

随机推荐