如:100000.01,结果必须为:壹拾万零一分
1000000000.01,结果必须为:壹拾亿零一分
注:二十万或者二十亿这些不用修改
代码如下,请标明修改的地方,谢谢!
- JScript code
function chineseNumber(num) { if (isNaN(num) || num > Math.pow(10, 12)) return "" var cn = "零壹贰叁肆伍陆柒捌玖" var unit = new Array("拾百千", "分角") var unit1= new Array("万亿", "") var numArray = num.toString().split(".") var start = new Array(numArray[0].length-1, 2) function toChinese(num, index) { var num = num.replace(/\d/g, function ($1) { return cn.charAt($1)+unit[index].charAt(start--%4 ? start%4 : -1) }) return num } for (var i=0; i<numArray.length; i++) { var tmp = "" for (var j=0; j*4<numArray[i].length; j++) { var strIndex = numArray[i].length-(j+1)*4 var str = numArray[i].substring(strIndex, strIndex+4) var start = i ? 2 : str.length-1 var tmp1 = toChinese(str, i) tmp1 = tmp1.replace(/(零.)+/g, "零").replace(/零+$/, "") tmp1 = tmp1.replace(/^壹拾/, "拾") tmp = (tmp1+unit1[i].charAt(j-1)) + tmp } numArray[i] = tmp } numArray[1] = numArray[1] ? numArray[1] : "" numArray[0] = numArray[0] ? numArray[0]+"元" : numArray[0], numArray[1] numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1]+"整" return (numArray[0]+numArray[1]).replace(/^零/, "") }
------解决方案--------------------
- HTML code
<script language="javascript"> function chineseNumber(num) { if (isNaN(num) || num > Math.pow(10, 12)) return "" var cn = "零壹贰叁肆伍陆柒捌玖" var unit = new Array("拾百千", "分角") var unit1= new Array("万亿", "") var numArray = num.toString().split(".") var start = new Array(numArray[0].length-1, 2) function toChinese(num, index) { var num = num.replace(/\d/g, function ($1) { return cn.charAt($1)+unit[index].charAt(start--%4 ? start%4 : -1) }) return num } for (var i=0; i<numArray.length; i++) { var tmp = "" for (var j=0; j*4<numArray[i].length; j++) { var strIndex = numArray[i].length-(j+1)*4 var str = numArray[i].substring(strIndex, strIndex+4) var start = i ? 2 : str.length-1 var tmp1 = toChinese(str, i) tmp1 = tmp1.replace(/(零.)+/g, "零").replace(/零+$/, "") //tmp1 = tmp1.replace(/^壹拾/, "拾") tmp = (tmp1+unit1[i].charAt(j-1)) + tmp } numArray[i] = tmp } numArray[1] = numArray[1] ? numArray[1] : "" numArray[0] = numArray[0] ? numArray[0]+"元" : numArray[0], numArray[1] numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1]+"整" return (numArray[0]+numArray[1]).replace(/^零/, "") } alert(chineseNumber(1111111111)) </script>
------解决方案--------------------
------解决方案--------------------
我自己写一个给你,看看符合你要求不?
- JScript code
function chineseNumber(num) { if (isNaN(num) || num > Math.pow(10, 12)) return ""; var CharBig = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); var CharBig2 = new Array("元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万"); var tmp = ""; var w=0; var n = Math.floor(num); while(n > 0){ var m = n % 10; n = Math.floor(n/10); tmp += CharBig2[w] + CharBig[m]; w +=1; } var result = "" for (var i=tmp.length - 1;i >= 0; i= i-2){ if (tmp.charAt(i) == "零") { if (tmp.charAt(i-1)=="万" || tmp.charAt(i-1)=="亿"|| tmp.charAt(i-1)=="元")result += tmp.charAt(i-1); continue; } else { result+=tmp.charAt(i) + tmp.charAt(i-1); } } result = result.replace("亿万", "亿"); if (result == "")result = "零元" result += CharBig[Math.floor(num * 10) % 10]+ "角"; result += CharBig[Math.floor(num*100) %10] + "分"; return result; }