javascript数字转化为汉字金额大写,可以处理负值
已经封装成类,直接复制使用
仅使用纯原生js编写,可以使用在浏览器、后端及任何js体系内
下面上
代码
,最后有
使用方法
//数字(数字类型或字符串类型)转化为金额大写(字符串类型)
class numToCapital {
uppercase(num) {
let minus = "";
let m_str = "";
const text = num.toString();
const zh = '零壹贰叁肆伍陆柒捌玖';
if (text.indexOf("-") === 0) {
num = text.replace("-", "");
minus = "负"
}
let money_num = Number(num);
let money = Math.round(money_num * 100).toString(10);
const len = money.length;
for (let i = 0; i < len; i++) {
m_str = m_str + zh.charAt(parseInt(money.charAt(i))) + this.sitToUnit(len - i - 1)
}
m_str = m_str.replace("零分", "");
m_str = m_str.replace("零角", "零");
let yy = 0;
while (true) {
let len = m_str.length;
m_str = m_str.replace("零圆", "圆");
m_str = m_str.replace("零万", "万");
m_str = m_str.replace("零亿", "亿");
m_str = m_str.replace("零仟", "零");
m_str = m_str.replace("零佰", "零");
m_str = m_str.replace("零零", "零");
m_str = m_str.replace("零拾", "零");
m_str = m_str.replace("亿万", "亿零");
m_str = m_str.replace("万仟", "万零");
m_str = m_str.replace("仟佰", "仟零");
yy = m_str.length;
if (yy === len) {
break
}
}
yy = m_str.length;
if (m_str.charAt(yy - 1) === "零") {
m_str = m_str.substring(0, yy - 1)
}
yy = m_str.length;
if (m_str.charAt(yy - 1) === "圆") {
m_str = m_str + "整"
}
return minus + m_str
}
sitToUnit(a) {
if (a > 10) {
a = a - 8;
return (this.sitToUnit(a))
}
const zh = '分角圆拾佰仟万拾佰仟亿';
return zh.charAt(a)
}
}
使用方法如下:
直接复制上下两端代码到script标签内即可运行
const amount_all1=123.45;
const amount_all2=12345678.90;
const ntc=new numToCapital();
console.log(ntc.uppercase(amount_all1));//壹佰贰拾叁圆肆角伍分
console.log(ntc.uppercase(amount_all2));//壹仟贰佰叁拾肆万伍仟陆佰柒拾捌圆玖角
版权声明:本文为ziqibit原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。