日期:2014-05-16  浏览次数:20523 次

急:请帮忙改多一个“零”,谢谢
今日在网上引用了一个js函数,是用于将阿拉伯数字金额转换为中文大写金额,如数字是12.05时,转换结果是:拾贰元五分,不过我们银行的会计人员说,正确必须是:拾贰元零五分!
由于本人对js只是知道一些皮毛,请各位高手帮忙,解决这个问题,谢谢!js函数如下,请改好并标注改动部分,谢谢!


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].replace(/^零+/, "")
numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1]+"整"
return numArray[0]+numArray[1]
}








------解决方案--------------------
Java 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].replace(/^零+/, "零")
numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1]+"整"
return numArray[0]+numArray[1]

}

alert(chineseNumber(12.05));
</script>

------解决方案--------------------
numArray[1] = numArray[1].replace(/^零+/, "零")
------解决方案--------------------
探讨

楼上两个都可以解决12.05的转换问题,不过如果金额是0.05,需要转换的结果是:伍分,而不是:零伍分,麻烦再帮我改改!谢谢!

------解决方案--------------------
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)
})