此方法为招商银行网上银行对金额进行大写的转换方法。
function ConvertUpperMoney(whole) {
//金额转换。输入数字字符串,低至分位,高位不为零
var GBK_unit1 = "分角";
var GBK_unit2 = "圆拾佰仟";
var GBK_unit3 = "万拾佰仟"
var GBK_unit4 = "亿拾佰仟";
var GBK_num = "零壹贰叁肆伍陆柒捌玖";
var section1 = "";
if (whole.length - 2 >= 0) section1 = whole.substr(whole.length - 2, 2);
else section1 = whole.substr(0, whole.length);
var section2 = "";
if (whole.length - 6 >= 0) section2 = whole.substr(whole.length - 6, 4);
else if (whole.length - 2 > 0) section2 = whole.substr(0, whole.length - 2);
var section3 = "";
if (whole.length - 6 > 0) section3 = whole.substr(0, whole.length - 6);
var str_GBK = "";
//分角处理
if (section1 == "") return str_GBK;
for (var i = 0; i < section1.length; i++) {
var n = section1.charAt(section1.length - 1 - i);
if (n != '0') str_GBK = GBK_num.substr(Number(n), 1) + GBK_unit1.substr(i, 1) + str_GBK;
else if (i == 1 && str_GBK.substr(0, 1) != "" && section2 != "") //角位加零
str_GBK = GBK_num.substr(Number(n), 1) + str_GBK;
}
//圆拾佰仟处理
if (str_GBK == "") str_GBK = "整";
if (section2 == "") return str_GBK;
for (var i = 0; i < section2.length; i++) {
var n = section2.charAt(section2.length - 1 - i);
if (n != '0') str_GBK = GBK_num.substr(Number(n), 1) + GBK_unit2.substr(i, 1) + str_GBK;
else if (i == 0) //圆位零字处理
str_GBK = GBK_unit2.substr(i, 1) + str_GBK;
else if (str_GBK.substr(0, 1) != "零" && str_GBK.substr(0, 1) != "圆") //拾佰仟位零字处理
str_GBK = GBK_num.substr(Number(n), 1) + str_GBK;
}
//万拾佰仟、亿拾佰仟循环处理
if (section3 == "") return str_GBK;
for (var i = 0,
k = 0; i < section3.length; i = i + 4, k = (k + 1) % 2) { var sectionSub = ""; var start = section3.length - (4 + i); if (start >= 0) {
sectionSub = section3.substr(start, 4);
} else {
sectionSub = section3.substr(0, 4 + start);
}
if (parseInt(sectionSub) == 0 && k == 0) //万拾佰仟节全为零跳过
{
continue;
}
for (var j = 0; j < sectionSub.length; j++) {
var n = sectionSub.charAt(sectionSub.length - 1 - j);
if (k == 0) //万拾佰仟处理
{
if (n != '0') str_GBK = GBK_num.substr(Number(n), 1) + GBK_unit3.substr(j, 1) + str_GBK;
else if (j == 0) //万位零字处理
str_GBK = GBK_unit3.substr(j, 1) + str_GBK;
else if (str_GBK.substr(0, 1) != "零" && str_GBK.substr(0, 1) != "万") //拾佰仟零字处理
str_GBK = GBK_num.substr(Number(n), 1) + str_GBK;
} else //亿拾佰仟处理
{
if (n != '0') str_GBK = GBK_num.substr(Number(n), 1) + GBK_unit4.substr(j, 1) + str_GBK;
else if (j == 0) //亿位零字处理
str_GBK = GBK_unit4.substr(j, 1) + str_GBK;
else if (str_GBK.substr(0, 1) != "零" && str_GBK.substr(0, 1) != "亿") //拾佰仟零字处理
str_GBK = GBK_num.substr(Number(n), 1) + str_GBK;
}
}
}
return str_GBK;
}
var Money = 50000000;
//传入函数的时候要转换为分
console.log(ConvertUpperMoney(String((Money * 100))));
转载请注明:清风亦平凡 » JavaScript实现金额转为大写