人民币大写转换

这是一个JavaScript函数,用于将人民币的小写金额转换为大写。它处理正负数,确保正确添加‘整’字,并能处理从分到万亿级别的金额。函数通过遍历数字字符串,结合汉字数字和单位来完成转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* 人民币大写转换
* @param {} numberValue 人民币小写
* @return {String}
*/
RMBCapital : function(numberValue) {
var numberValue = new String(Math.round(numberValue * 100)); // 数字金额
var isNegative = false;
if (numberValue.indexOf(“-“) == 0) { // 检测是否是负数
isNegative = true;
numberValue = numberValue.substr(1, numberValue.length);
}
var chineseValue = “”; // 转换后的汉字金额
var String1 = “零壹贰叁肆伍陆柒捌玖”; // 汉字数字
var String2 = “万仟佰拾亿仟佰拾万仟佰拾元角分”; // 对应单位
var len = numberValue.length; // numberValue 的字符串长度
var Ch1; // 数字的汉语读法
var Ch2; // 数字位的汉字读法
var nZero = 0; // 用来计算连续的零值的个数
var String3; // 指定位置的数值
if (len > 15) {
throw new Error(“超出计算范围”);
return “”;
}
if (numberValue == 0) {
chineseValue = “零元整”;
return chineseValue;
}
// 取出对应位数的STRING2的值
String2 = String2.substr(String2.length - len, len);
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 + “整”;
}
if (isNegative) {
return “负” + chineseValue;
}
return chineseValue;
}
};

数字转成人民币大写代码 Public Function GetChinaNum(otherNum As Double, Optional isRMB As Boolean, Optional numOption As Boolean, Optional dotNum As Integer) As String On Error Resume Next num = Trim(Str(Int(otherNum))) If isRMB Then numwei = "拾佰仟万拾佰仟亿拾佰仟" numshu = "零壹贰叁肆伍陆柒捌玖拾" Else numwei = "十百千万十百千亿十百千" numshu = "零一二三四五六七八九十" End If If otherNum < 20 And otherNum >= 10 Then num = Right(num, 1) GetChinaNum = Left(numwei, 1) End If For i = 1 To Len(num) bstr = Mid(num, i, 1) If numOption Then GetChinaNum = GetChinaNum + Mid(numshu, Val(bstr) + 1, 1) Else GetChinaNum = GetChinaNum + Mid(numshu, Val(bstr) + 1, 1) If bstr = "0" Then If Mid(numwei, Len(num) - i, 1) = "万" Or Mid(numwei, Len(num) - i, 1) = "亿" Then Do While Right(GetChinaNum, 1) = "零" GetChinaNum = Left(GetChinaNum, Len(GetChinaNum) - 1) Loop GetChinaNum = GetChinaNum + Mid(numwei, Len(num) - i, 1) End If Else GetChinaNum = GetChinaNum + Mid(numwei, Len(num) - i, 1) End If GetChinaNum = Replace(GetChinaNum, "零零", "零") End If Next i If numOption = False Then Do While Right(GetChinaNum, 1) = "零" GetChinaNum = Left(GetChinaNum, Len(GetChinaNum) - 1) Loop End If If isRMB Then numrmb = "元角分" GetChinaNum = GetChinaNum + Mid(numrmb, 1, 1) If Val(num) <> otherNum Then num = Trim(Str(Round(otherNum - Val(num), 2))) For i = 2 To Len(num) bstr = Mid(num, i, 1) GetChinaNum = GetChinaNum + Mid(numshu, Val(bstr) + 1, 1) + Mid(numrmb, i, 1) Next i Else GetChinaNum = GetChinaNum + "整" End If Else If Val(num) <> otherNum Then If dotNum = 0 Then dotNum = 4 num = Trim(CStr(Round(otherNum - Val(num), dotNum))) If GetChinaNum = "" Then GetChinaNum = "零" GetChinaNum = GetChinaNum + "点" For i = 2 To Len(num) bstr = Mid(num, i, 1) GetChinaNum = GetChinaNum + Mid(numshu, Val(bstr) + 1, 1) Next i End If End If End Function
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值