很多借贷公司或者是网上有大笔交易的公司,总是需要在PC端部署一个协议或者是合同,合同所涉及金额,一般正规一点的合同通常是大写人民币,如果你给别人开过收据,你就明白了。
那么到底如何将数据库拿到的数字即时转换成大写呢?
忘记在哪个网站看到的代码了,如果涉及侵权,请私信告知:
//获取元素内容
var moneys =$(".money").text();
function Arabia_To_SimplifiedChinese(Num) {
for (i = Num.length - 1; i >= 0; i--) {
Num = Num.replace(",", "")//替换Num中的“,”
Num = Num.replace(" ", "")//替换Num中的空格
}
//验证输入的字符是否为数字
if (isNaN(Num)) {
alert("请检查小写金额是否正确");
return;
}
//字符处理完毕后开始转换,采用前后两部分分别转换(小数点为界限)
part = String(Num).split(".");
// 初始化一个字符串
newchar = "";
//小数点前进行转化
for (i = part[0].length - 1; i >= 0; i--) {
//若数量超过拾亿单位,提示
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;
}
//替换所有无用汉字,直到没有此类无用的数字为止
while (newchar.search("零零") != -1 || newchar.search("零亿") != -1 || newchar.search("亿万") != -1 || newchar.search("零万") != -1) {
newchar = newchar.replace("零亿", "亿");
newchar = newchar.replace("亿万", "亿");
newchar = newchar.replace("零万", "万");
newchar = newchar.replace("零零", "零");
}
//替换以“一十”开头的,为“十”
if (newchar.indexOf("一十") == 0) {
newchar = newchar.substr(1);
}
//替换以“零”结尾的,为“”
if (newchar.lastIndexOf("零") == newchar.length - 1) {
newchar = newchar.substr(0, newchar.length - 1);
}
return newchar;
}
//调用函数,传值
Arabia_To_SimplifiedChinese(moneys)
//将函数返回值给元素重新赋值
$(".money").text(newchar);
有帮助记得点赞~