1 var convertCurrent = function(currencyDigits){ 2 var MAXIMUM_NUMBER = 99999999999.99; //设置最大值 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; //Represent integral part of digit number 26 var decimal; //Represent decimal part of digit number 27 var outputCharacters; //The output result 28 var parts; 29 var digits, radices, bigRadices, decimals; 30 var zeroCount; 31 var i, p, d; 32 var quotient, modulus; 33 34 currencyDigits = currencyDigits.toString(); 35 if(currencyDigits == ""){ 36 return "请输入金额!"; 37 } 38 if(currencyDigits.match(/[^,.\d]/) != null){ 39 return "请输入有效数字"; 40 } 41 if(currencyDigits.match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/) == null){ 42 return "数字格式错误"; 43 } 44 currencyDigits = currencyDigits.replace(/,/g, ""); //Remove comma delimiters 45 currencyDigits = currencyDigits.replace(/^0+/, ""); //Trim zeros at the beginning 46 if(Number(currencyDigits) > MAXIMUM_NUMBER){ 47 return "您输入的数字太大了"; 48 } 49 parts = currencyDigits.split("."); 50 if(parts.length>1){ 51 integral = parts[0]; 52 decimal = parts[1]; 53 decimal = decimal.substr(0,2); //限制两位有效数字 54 }else{ 55 integral = parts[0]; 56 decimal = ""; 57 } 58 digits = new Array(CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE); 59 radices = new Array("", CN_TEN, CN_HUNDRED, CN_THOUSAND); 60 bigRadices = new Array("", CN_TEN_THOUSAND, CN_HUNDRED_MILLION); 61 decimals = new Array(CN_TEN_CENT, CN_CENT); 62 outputCharacters = ""; 63 if(Number(integral) > 0){ 64 zeroCount = 0; 65 for(i=0;i<integral.length;i++){ 66 p = integral.length-i-1; 67 d = integral.substr(i,1); 68 quotient = p / 4; 69 modulus = p % 4; 70 if(d == "0"){ 71 zeroCount++; 72 }else{ 73 if(zeroCount>0){ 74 outputCharacters += digits[0]; 75 } 76 zeroCount = 0; 77 outputCharacters += digits[Number(d)] + radices[modulus]; 78 } 79 if(modulus == 0 && zeroCount < 4){ 80 outputCharacters += bigRadices[quotient]; 81 } 82 } 83 outputCharacters += CN_DOLLAR; 84 } 85 if(decimal != ""){ 86 for(i=0;i<decimal.length;i++){ 87 d = decimal.substr(i,1); 88 outputCharacters += digits[Number(d)] + decimals[i]; 89 } 90 } 91 if(outputCharacters == ""){ 92 outputCharacters = CN_ZERO + CN_DOLLAR; 93 } 94 if(decimal == ""){ 95 outputCharacters += CN_INTEGER; 96 } 97 outputCharacters = outputCharacters; 98 return outputCharacters; 99 }