JavaScript 数字转汉字大写 (JS金额大写)

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));//壹仟贰佰叁拾肆万伍仟陆佰柒拾捌圆玖角
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ziqibit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值