可以通过缩放来进行分到元的转换,同时使用正则对处理后的数字进行千分位格式化
num=num*0.01//分到元
num+=''//转成字符串
reg=num.indexOf('.') >-1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g//确定使用有哪个正则
num=num.replace(reg, '$1,')//千分位格式化
经过上述4步,即可完成分到元的转换,并且有千分位的格式化
<script type="text/javascript">function money(mon){
var smon = Math.round(mon*100)
var splot = smon % 100
var zheng = parseInt(smon / 100)
var ch_key = new Array('零','壹','贰','叁','肆','伍','陆','柒','捌','玫')
var dan_key = new Array('拾','佰','仟','万','拾万','百万','仟万','亿')
var str_num = zheng+''
var len = str_num.length
var slen = len-1
var rs = ''
for(var i = 0i<leni++){
var ch = parseInt(str_num.charAt(i))
rs += ch_key[ch]
slen --
if(ch >0 &&slen >= 0) rs += dan_key[slen]
}
if(splot == 0){
rs += '元整'
}else{
var str = splot + ''
var ch = parseInt(str.charAt(0))
rs += '点' + ch_key[ch]
ch = parseInt(str.charAt(1))
rs += ch_key[ch] + '元'
}
return rs
}
alert(money(211199.999))
</script>
百度一下这种写好的js很多 随便选个就可以我帮你搜了个function Arabia_to_Chinese(Num){
for(i=Num.length-1i>=0i--)
{
Num = Num.replace(",","")
Num = Num.replace(" ","")
}
Num = Num.replace("¥","")
if(isNaN(Num)) {
alert("请检查小写金额是否正确")
return
}
part = String(Num).split(".")
newchar = ""
for(i=part[0].length-1i>=0i--){
if(part[0].length >10){ alert("位数过大,无法计算")return ""}
tmpnewchar = ""
perchar = part[0].charAt(i)
switch(perchar){
case "0": tmpnewchar="零" + tmpnewchar break
case "1": tmpnewchar="壹" + tmpnewchar break
case "2": tmpnewchar="贰" + tmpnewchar break
case "3": tmpnewchar="叁" + tmpnewchar break
case "4": tmpnewchar="肆" + tmpnewchar break
case "5": tmpnewchar="伍" + tmpnewchar break
case "6": tmpnewchar="陆" + tmpnewchar break
case "7": tmpnewchar="柒" + tmpnewchar break
case "8": tmpnewchar="捌" + tmpnewchar break
case "9": tmpnewchar="玖" + tmpnewchar break
}
switch(part[0].length-i-1){
case 0: tmpnewchar = tmpnewchar +"元" break
case 1: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" break
case 2: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" break
case 3: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" break
case 4: tmpnewchar= tmpnewchar +"万" break
case 5: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" break
case 6: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" break
case 7: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" break
case 8: tmpnewchar= tmpnewchar +"亿" break
case 9: tmpnewchar= tmpnewchar +"拾" break
}
newchar = tmpnewchar + newchar
}
if(Num.indexOf(".")!=-1){
if(part[1].length >2) {
part[1] = part[1].substr(0,2)
}
for(i=0i<part[1].lengthi++){
tmpnewchar = ""
perchar = part[1].charAt(i)
switch(perchar){
case "0": tmpnewchar="零" + tmpnewchar break
case "1": tmpnewchar="壹" + tmpnewchar break
case "2": tmpnewchar="贰" + tmpnewchar break
case "3": tmpnewchar="叁" + tmpnewchar break
case "4": tmpnewchar="肆" + tmpnewchar break
case "5": tmpnewchar="伍" + tmpnewchar break
case "6": tmpnewchar="陆" + tmpnewchar break
case "7": tmpnewchar="柒" + tmpnewchar break
case "8": tmpnewchar="捌" + tmpnewchar break
case "9": tmpnewchar="玖" + tmpnewchar break
}
if(i==0)tmpnewchar =tmpnewchar + "角"
if(i==1)tmpnewchar = tmpnewchar + "分"
newchar = newchar + tmpnewchar
}
}
while(newchar.search("零零") != -1)
newchar = newchar.replace("零零", "零")
newchar = newchar.replace("零亿", "亿")
newchar = newchar.replace("亿万", "亿")
newchar = newchar.replace("零万", "万")
newchar = newchar.replace("零元", "元")
newchar = newchar.replace("零角", "")
newchar = newchar.replace("零分", "")
if (newchar.charAt(newchar.length-1) == "元" || newchar.charAt(newchar.length-1) == "角")
newchar = newchar+"整"
return newchar
}
看了下没什么大问题