可以通过缩放来进行分到元的转换,同时使用正则对处理后的数字进行千分位格式化
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步,即可完成分到元的转换,并且有千分位的格式化
//小写转成大写金额function AmountLtoU(num){
var strOutput = ""
var strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分'
num += "00"
var intPos = num.indexOf('.')
if (intPos >= 0)
num = num.substring(0, intPos) + num.substr(intPos + 1, 2)
strUnit = strUnit.substr(strUnit.length - num.length)
for (var i=0 i < num.length i++)
strOutput += '零壹贰叁肆伍陆柒捌玖'.substr(num.substr(i,1),1) + strUnit.substr(i,1)
return strOutput.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([亿|万])/g, '$1').replace(/零+元/, '元').replace(/亿零{0,3}万/, '亿').replace(/^元/, "零元")
<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>