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
    评论
<script type="text/javascript"> function CheckAlls(checked) { len = document.frmmain.elements.length; var i=0; for( i=0; i<len; i++) { if (document.frmmain.elements[i].name=='check_node_0') { if(document.frmmain.elements[i].disabled == false){ document.frmmain.elements[i].checked=(checked==true?true:false); } } } } </script><script> document.onpropertychange= function() { var a= document.getElementById('field6163').value;//取小写金额的值 var numberValue=new String(Math.round(a*100)); // 数字金额 var String1 = "零壹贰叁肆伍陆柒捌玖"; // 汉字数字 var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; // 对应单位 var len=numberValue.length; // numberValue 的字符串长度 //alert(len); var Ch1; // 数字的汉语读法 var Ch2; // 数字位的汉字读法 var nZero=0; // 用来计算连续的零值的个数 var String3; // 指定位置的数值 { if(len>15){ alert("超出计算范围"); return fase; } if (numberValue==0){ chineseValue = "零元整"; } else { chineseValue=""; } String2 = String2.substr(String2.length-len, len); // 取出对应位数的STRING2的值 for(var i=0; i<len; i++){ String3 = parseInt(numberValue.substr(i, 1),10); // 取出需换的某一位的值 if ( i != (len - 3) && i != (len - 7) && i != (len - 11) && i !=(len - 15) ){ if ( String3 == 0 ){ Ch1 = ""; Ch2 = ""; nZero = nZero + 1; } else if ( String3 != 0 && nZero != 0 ){ Ch1 = "零" + String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else{ Ch1 = String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } } else{ // 该位是万亿,亿,万,元位等关键位 if( String3 != 0 && nZero != 0 ){ Ch1 = "零" + String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else if ( String3 != 0 && nZero == 0 ){ Ch1 = String1.substr(String3, 1); Ch2 = String2.substr(i, 1); nZero = 0; } else if( String3 == 0 && nZero >= 3 ){ Ch1 = ""; Ch2 = ""; nZero = nZero + 1; } else{ Ch1 = ""; Ch2 = String2.substr(i, 1); nZero = nZero + 1; } if( i == (len - 11) || i == (len - 3)){ // 如果该位是亿位或元位,则必须写上 Ch2 = String2.substr(i, 1); } } chineseValue =chineseValue +Ch1 + Ch2; } if ( String3 == 0 ){ // 最后一位(分)为0时,加上“整” chineseValue = chineseValue + "整"; } } //alert(chineseValue); document.getElementById('field6421').value=chineseValue; } </script>
好的,我可以回答这个问题。以下是将数字金额换为中文大写金额JavaScript代码: ```javascript function convertToChineseNumeral(amount) { const chineseNumeral = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; const chineseUnit = ["", "拾", "佰", "仟"]; const chineseBigUnit = ["", "萬", "亿", "兆"]; let num = parseFloat(amount); let chineseAmount = ""; if (isNaN(num)) { return ""; } let isNegative = num < 0; if (isNegative) { num = -num; } let fraction = Math.round((num - Math.floor(num)) * 100); if (fraction == 0) { chineseAmount += "整"; } else { chineseAmount += chineseNumeral[Math.floor(fraction / 10)] + "角"; chineseAmount += chineseNumeral[fraction % 10] + "分"; } num = Math.floor(num); let unitIndex = 0; while (num > 0) { let currentUnit = ""; for (let i = 0; i < 4 && num > 0; i++) { let currentNumeral = num % 10; if (currentNumeral > 0) { currentUnit = chineseUnit[i] + currentUnit; currentUnit = chineseNumeral[currentNumeral] + currentUnit; } else if (currentUnit != "") { currentUnit = chineseNumeral[currentNumeral] + currentUnit; } num = Math.floor(num / 10); } if (currentUnit != "") { currentUnit += chineseBigUnit[unitIndex]; } chineseAmount = currentUnit + chineseAmount; unitIndex++; } if (chineseAmount == "") { chineseAmount = chineseNumeral[0]; } if (isNegative) { chineseAmount = "负" + chineseAmount; } return chineseAmount; } console.log(convertToChineseNumeral(123456789.01)); // 壹亿贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖元零壹分 ``` 希望这个代码能帮到你。如果你有其它问题,欢迎继续询问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ziqibit

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

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

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

打赏作者

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

抵扣说明:

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

余额充值