1 function convertCurrency(currencyDigits) { 2 var MAXIMUM_NUMBER = 1000000000000.00; 3 4 var CN_ZERO = "零"; 5 var CN_ONE = "壹"; 6 var CN_TWO = "贰"; 7 var CN_THREE = "叁"; 8 var CN_FOUR = "肆"; 9 var CN_FIVE = "伍"; 10 var CN_SIX = "陆"; 11 var CN_SEVEN = "柒"; 12 var CN_EIGHT = "捌"; 13 var CN_NINE = "玖"; 14 var CN_TEN = "拾"; 15 var CN_HUNDRED = "佰"; 16 var CN_THOUSAND = "仟"; 17 var CN_TEN_THOUSAND = "万"; 18 var CN_HUNDRED_MILLION = "亿"; 19 var CN_SYMBOL = "人民币"; 20 var CN_DOLLAR = "元"; 21 var CN_TEN_CENT = "角"; 22 var CN_CENT = "分"; 23 var CN_INTEGER = "整"; 24 25 var integral; // integral part 26 var decimal; // decimal part 27 var outputCharacters = ""; // result. 28 var parts; 29 var digits, radices, bigRadices, decimals; 30 var zeroCount; 31 var i, p, d; 32 var quotient, modulus; 33 34 if (currencyDigits != null) { 35 currencyDigits = currencyDigits.toString(); 36 currencyDigits = currencyDigits.replace(/,/g, ""); // Remove comma delimiters. 37 currencyDigits = currencyDigits.replace(/^0+/, ""); // Trim zeros at the beginning. 38 39 if (Number(currencyDigits) < MAXIMUM_NUMBER) { 40 if (Number(currencyDigits) < 0) { 41 outputCharacters += "负"; 42 currencyDigits = Math.abs(Number(currencyDigits)).toString(); 43 } 44 45 parts = currencyDigits.split("."); // separate the number by decimal point 46 if (parts.length > 1) { 47 integral = parts[0]; 48 decimal = parts[1]; 49 decimal = decimal.substr(0, 2);// two decimal places 50 } else { 51 integral = parts[0]; 52 decimal = ""; 53 } 54 55 digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE); 56 radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND); 57 bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION); 58 decimals = new Array(CN_TEN_CENT, CN_CENT); 59 60 if (Number(integral) > 0) { 61 zeroCount = 0; 62 for (i = 0; i < integral.length; i++) { 63 p = integral.length - i - 1; 64 d = integral.substr(i, 1); 65 quotient = p / 4; 66 modulus = p % 4; 67 if (d == "0") { 68 zeroCount++; 69 } else { 70 if (zeroCount > 0) { 71 outputCharacters += digits[0]; 72 } 73 zeroCount = 0; 74 outputCharacters += digits[Number(d)] + radices[modulus]; 75 } 76 if (modulus == 0 && zeroCount < 4) { 77 outputCharacters += bigRadices[quotient]; 78 } 79 } 80 outputCharacters += CN_DOLLAR; 81 } 82 83 if (decimal != "") { 84 for (i = 0; i < decimal.length; i++) { 85 d = decimal.substr(i, 1); 86 if (d != "0") { 87 outputCharacters += digits[Number(d)] + decimals[i]; 88 } 89 } 90 } 91 if (outputCharacters == "") { 92 outputCharacters = CN_ZERO + CN_DOLLAR; 93 } 94 if (decimal == "") { 95 outputCharacters += CN_INTEGER; 96 } 97 outputCharacters = "" + outputCharacters + ""; 98 } 99 } 100 return outputCharacters; 101 }