人民币金额大写的程序应该如何写?
人工智能写的代码太弱智了。这样的代码应该如何写?具体流程是?
我目前能想到的办法就是穷举法!
但是穷举法太慢了。
比如
123456.78得到
壹拾贰万叁仟肆佰伍拾陆元柒角捌分
要是能像搜狗拼音一样就好了,
人工智能写的太差了。 https://blog.51cto.com/u_16213310/12989775
这儿有代码,看起来比人工智能写的靠谱多了 将人民币金额数值转换为大写的算法可以通过以下步骤实现:
整数部分处理
先将金额的整数部分从右至左每四位分为一组,分别是个、万、亿等数级。例如,123456789.12可分为1|2345|6789,分别对应亿级、万级、个级。
从高位开始,对每一组数进行处理。对于每一组,按照个、十、百、千的顺序,将数字转换为对应的大写汉字,如0对应“零”,1对应“壹”,2对应“贰”等。例如,“2345”转换为“贰仟叁佰肆拾伍”。
在每一组数的后面加上相应的数级单位,如“亿”“万”等。但如果该组数字全为0,则省略该数级单位,不过当万级全为0时,要根据亿级和个级的情况来决定是否写“零”。例如,100005000,写成“壹亿零伍仟”。
小数部分处理
对于小数部分,将十分位和百分位的数字分别转换为大写汉字,如0.12转换为“壹角贰分”。如果十分位是0,可写成“零X分”,如0.05写成“零伍分”。
特殊情况处理
金额为0时,直接写成“零元整”。
当金额整数部分末尾是0,且小数部分为0时,要在整数部分大写后加上“整”字,如1000.00写成“壹仟元整”。
以下是一个Python示例代码实现该算法:
python
def convert_to_chinese_uppercase(amount):
# 数字对应的大写汉字
num_dict = {0: '零', 1: '壹', 2: '贰', 3: '叁', 4: '肆', 5: '伍', 6: '陆', 7: '柒', 8: '捌', 9: '玖'}
# 数级单位
unit_dict = ['', '拾', '佰', '仟', '万', '亿']
integer_part, decimal_part = str(amount).split('.')
integer_part = int(integer_part)
decimal_part = int(decimal_part)
if integer_part == 0 and decimal_part == 0:
return '零元整'
chinese_uppercase = ''
# 处理整数部分
if integer_part > 0:
integer_str = str(integer_part)[::-1]
for i in range(0, len(integer_str), 4):
group = integer_str[::-1]
group_uppercase = ''
for j, num in enumerate(group):
num = int(num)
if num!= 0:
group_uppercase += num_dict + unit_dict
if group_uppercase!= '':
if i // 4 == 1 and all(int(x) == 0 for x in integer_str):
chinese_uppercase = '万' + chinese_uppercase
else:
chinese_uppercase = group_uppercase + unit_dict + chinese_uppercase
# 处理小数部分
if decimal_part > 0:
chinese_uppercase +='元'
if decimal_part // 10 > 0:
chinese_uppercase += num_dict + '角'
if decimal_part % 10 > 0:
chinese_uppercase += num_dict + '分'
else:
chinese_uppercase +='元整'
return chinese_uppercase
# 测试函数
amount = 123456789.12
print(convert_to_chinese_uppercase(amount))
上述代码定义了一个函数 convert_to_chinese_uppercase ,它接受一个浮点数作为参数,将其转换为人民币大写金额的字符串并返回。代码中使用了两个字典 num_dict 和 unit_dict 来存储数字和数级单位的对应关系。先将金额分为整数部分和小数部分,然后分别对它们进行处理,最后将处理结果组合起来返回。 搜狗输入法,v模式
输入v10^20.5,得到
10^20.5=3.16227766017e+20
三万一千六百二十二亿七千七百六十六万零一百六十八亿三千七百九十四万三千二百九十六元整
叁万壹仟陆佰贰拾贰亿柒仟柒佰陆拾陆万零壹佰陆拾捌亿叁仟柒佰玖拾肆万叁仟贰佰玖拾陆元整
为什么我感觉这个输出结果是错误的? nyy 发表于 2025-2-26 09:58
搜狗输入法,v模式
输入v10^20.5,得到
10^20.5=3.16227766017e+20
我发现了搜狗输入法的一个bug
输入
10^20.5 // IntegerPart
得到
316227766016837943296
输入
10^(41/2) // IntegerPart
得到
316227766016837933199
搜狗输入法,最后五位计算结果不正确 万、亿,后面的是?
万、亿后面的计数单位还有:
- 兆:1兆等于1万亿。
- 京:1京等于1万兆。
- 垓:1垓等于1万京。
- 秭:1秭等于1万垓。
- 穰:1穰等于1万秭。
- 沟:1沟等于1万穰。
- 涧:1涧等于1万沟。
- 正:1正等于1万涧。
- 载:1载等于1万正。
计数单位还有极、恒河沙、阿僧祇、那由他、不可思议、无量、大数等,且不同的文化和领域可能会有不同的使用习惯和更大的计数单位。
这个是人工智能的回答,也许有用 以下是这些计数单位与科学计数法的对应:
万:1万 = 10^{4}
亿:1亿 = 10^{8}
兆:1兆 = 10^{12}
京:1京 = 10^{16}
垓:1垓 = 10^{20}
秭:1秭 = 10^{24}
穰:1穰 = 10^{28}
沟:1沟 = 10^{32}
涧:1涧 = 10^{36}
正:1正 = 10^{40}
载:1载 = 10^{44}
如果继续往后,极对应的是10^{48},恒河沙是10^{52},阿僧祇是10^{56},那由他是10^{60},不可思议是10^{64},无量是10^{68},大数是10^{72}。
我感觉搜狗输入法结果不对 本帖最后由 nyy 于 2025-2-27 11:53 编辑
nyy 发表于 2025-2-26 14:26
以下是这些计数单位与科学计数法的对应:
万:1万 = 10^{4}
'逆转字符串功能,输入abcd,则返回dcba
Function ReverseString(ByVal str As String) As String
Dim i As Integer
Dim result As String
'默认结果初始值
result=""
'如果字符串的长度等于零,则返回空字符串
if Len(str)=0 then
ReverseString=str
Exit Function
end if
' 遍历原字符串,从最后一个字符开始
For i = Len(str) To 1 Step -1
' 将当前字符添加到结果字符串中
result = result & Mid(str, i, 1)
Next i
ReverseString = result
End Function
' 函数名称:NumToChineseMoney
' 功能:将输入的人民币数字金额转换为大写金额
' 输入参数:
' num:要转换的数字金额,可以是整数或小数类型
' 返回参数:
' 转换后的大写金额字符串
Function NumToChineseMoney(ByVal num As Variant) As String
' 存储数字 0 - 9 对应的大写汉字
Dim numDict(0 To 9) As String
' 存储个、十、百、千单位
Dim unitDict(0 To 19) As String
' 存储输入数字的整数部分的字符串形式
Dim integerPartStr As String
' 存储输入数字的小数部分的字符串形式
Dim decimalPartStr As String
' 存储转换后的整数部分大写金额
Dim integerPart As String
' 存储转换后的小数部分大写金额
Dim decimalPart As String
' 循环计数器,用于遍历数字字符串
Dim i As Integer
' 存储当前处理的数字
Dim digit As Integer
''''''''''''''''''''''''''''''''''''''''''
' 初始化数字对应的大写汉字
numDict(0) = "零"
numDict(1) = "壹"
numDict(2) = "贰"
numDict(3) = "叁"
numDict(4) = "肆"
numDict(5) = "伍"
numDict(6) = "陆"
numDict(7) = "柒"
numDict(8) = "捌"
numDict(9) = "玖"
'将来扩展用
' 空 拾 佰 仟 万 1万 = 10^{4}
' 拾 佰 仟 亿 1亿 = 10^{8}
' 拾 佰 仟 兆 1兆 = 10^{12}
' 拾 佰 仟 京 1京 = 10^{16}
' 拾 佰 仟 垓 1垓 = 10^{20}
' 拾 佰 仟 秭 1秭 = 10^{24}
' 拾 佰 仟 穰 1穰 = 10^{28}
' 拾 佰 仟 沟 1沟 = 10^{32}
' 拾 佰 仟 涧 1涧 = 10^{36}
' 拾 佰 仟 正 1正 = 10^{40}
' 拾 佰 仟 载 1载 = 10^{44}
'初始化个、十、百、千单位
unitDict(0)=""
unitDict(1)="拾"
unitDict(2)="佰"
unitDict(3)="仟"
unitDict(4)="万"
unitDict(5)="拾"
unitDict(6)="佰"
unitDict(7)="仟"
unitDict(8)="亿"
unitDict(9)="拾"
unitDict(10)="佰"
unitDict(11)="仟"
unitDict(12)="兆"
unitDict(13)="拾"
unitDict(14)="佰"
unitDict(15)="仟"
unitDict(16)="京"
unitDict(17)="拾"
unitDict(18)="佰"
unitDict(19)="仟"
''''''''''''''''''''''''''''''''''''''''''
' 处理小数部分
If (num-int(num)<>0)Then
' 将输入数字转换为字符串
Dim numStr As String
numStr = CStr(num)
' 查找小数点的位置
Dim dotPos As Integer
dotPos = InStr(numStr, ".")
' 提取整数部分的字符串
integerPartStr = Mid(numStr, 1, dotPos - 1)
' 提取小数部分的字符串
decimalPartStr = Mid(numStr, dotPos + 1)
' 确保小数部分为两位,不足两位补零
decimalPartStr = Mid(decimalPartStr & "00", 1, 2)
' 如果小数部分不全为零,则进行转换
If decimalPartStr <> "00" Then
digit = Val(Mid(decimalPartStr, 1, 1))
If digit <> 0 Then
decimalPart = decimalPart & numDict(digit) & "角"
End If
digit = Val(Mid(decimalPartStr, 2, 1))
If digit <> 0 Then
decimalPart = decimalPart & numDict(digit) & "分"
End If
End If
Else
' 如果输入是整数,直接将其转换为字符串作为整数部分
integerPartStr= CStr(num)
End If
''''''''''''''''''''''''''''''''''''''''''
' 处理整数部分
if Len(integerPartStr)>=20 then
MsgBox "数字太大,2024年中国GDP才134.91万亿元"
NumToChineseMoney="数字太大,2024年中国GDP才134.91万亿元"
Exit Function
End If
'把整数部分的字符串逆转,用循环加入单位
mystr=ReverseString(integerPartStr) '整数部分逆转后的字符串
integerPart=""
For i=0 to Len(mystr)-1 step 1
digit=val(Mid(mystr,i+1,1)) '这儿需要加1,否则bug
integerPart=numDict(digit) & unitDict(i) & integerPart
Next i
integerPart=Replace(integerPart,"零拾","零")
integerPart=Replace(integerPart,"零佰","零")
integerPart=Replace(integerPart,"零仟","零")
'把两个以上的零,合并成一个零(替换20次,应该够了)
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
integerPart=Replace(integerPart,"零零","零")
'这些单位,放在后面替换,特殊
integerPart=Replace(integerPart,"零万","万")
integerPart=Replace(integerPart,"零亿","亿")
integerPart=Replace(integerPart,"零兆","兆")
integerPart=Replace(integerPart,"零京","京")
''''''''''''''''''''''''''''''''''''''''''
If integerPart = "" Then
' 如果整数部分为空,设为零
integerPart = "零"
End If
' 添加元
integerPart = integerPart & "元"
''''''''''''''''''''''''''''''''''''''''''
' 合并整数部分和小数部分
NumToChineseMoney = integerPart & decimalPart
If decimalPart = "" Then
' 如果没有小数部分,添加整字
NumToChineseMoney = NumToChineseMoney & "整"
End If
End Function
Sub TestNumToChineseMoney()
Dim number As Double
Dim result As String
' 示例 1:整数
number = 1000000001.23
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 2:带小数
number = 123.45
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 3:小数为零
number = 123.00
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 4:整数为零
number = 0.45
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
End Sub
先传一个有bug的代码上来,主要是零的处理的问题,
比如
1000000001 壹拾亿万零壹元整
12345678.12 壹仟贰佰叁拾肆万伍仟陆佰柒拾捌元壹角贰分
不知道什么时候能优化好!
1000000001 壹拾亿万零壹元整
这个结果显然错误。 nyy 发表于 2025-2-27 11:34
先传一个有bug的代码上来,主要是零的处理的问题,
比如
'逆转字符串功能,输入abcd,则返回dcba
Function ReverseString(ByVal str As String) As String
Dim i As Integer
Dim result As String
'默认结果初始值
result=""
'如果字符串的长度等于零,则返回空字符串
if Len(str)=0 then
ReverseString=str
Exit Function
end if
' 遍历原字符串,从最后一个字符开始
For i = Len(str) To 1 Step -1
' 将当前字符添加到结果字符串中
result = result & Mid(str, i, 1)
Next i
ReverseString = result
End Function
' 函数名称:NumToChineseMoney
' 功能:将输入的人民币数字金额转换为大写金额
' 输入参数:
' num:要转换的数字金额,可以是整数或小数类型
' 返回参数:
' 转换后的大写金额字符串
Function NumToChineseMoney(ByVal num As Variant) As String
' 存储数字 0 - 9 对应的大写汉字
Dim numDict(0 To 9) As String
' 存储个、十、百、千单位
Dim unitDict(0 To 19) As String
' 存储输入数字的整数部分的字符串形式
Dim integerPartStr As String
' 存储输入数字的小数部分的字符串形式
Dim decimalPartStr As String
' 存储转换后的整数部分大写金额
Dim integerPart As String
' 存储转换后的小数部分大写金额
Dim decimalPart As String
' 循环计数器,用于遍历数字字符串
Dim i As Integer
' 存储当前处理的数字
Dim digit As Integer
''''''''''''''''''''''''''''''''''''''''''
' 初始化数字对应的大写汉字
numDict(0) = "零"
numDict(1) = "壹"
numDict(2) = "贰"
numDict(3) = "叁"
numDict(4) = "肆"
numDict(5) = "伍"
numDict(6) = "陆"
numDict(7) = "柒"
numDict(8) = "捌"
numDict(9) = "玖"
'将来扩展用
' 空 拾 佰 仟 万 1万 = 10^{4}
' 拾 佰 仟 亿 1亿 = 10^{8}
' 拾 佰 仟 兆 1兆 = 10^{12}
' 拾 佰 仟 京 1京 = 10^{16}
' 拾 佰 仟 垓 1垓 = 10^{20}
' 拾 佰 仟 秭 1秭 = 10^{24}
' 拾 佰 仟 穰 1穰 = 10^{28}
' 拾 佰 仟 沟 1沟 = 10^{32}
' 拾 佰 仟 涧 1涧 = 10^{36}
' 拾 佰 仟 正 1正 = 10^{40}
' 拾 佰 仟 载 1载 = 10^{44}
'初始化个、十、百、千单位
unitDict(0)=""
unitDict(1)="拾"
unitDict(2)="佰"
unitDict(3)="仟"
unitDict(4)="万" '特殊
unitDict(5)="拾"
unitDict(6)="佰"
unitDict(7)="仟"
unitDict(8)="亿" '特殊
unitDict(9)="拾"
unitDict(10)="佰"
unitDict(11)="仟"
unitDict(12)="兆" '特殊
unitDict(13)="拾"
unitDict(14)="佰"
unitDict(15)="仟"
unitDict(16)="京" '特殊
unitDict(17)="拾"
unitDict(18)="佰"
unitDict(19)="仟"
''''''''''''''''''''''''''''''''''''''''''
' 处理小数部分
If (num-int(num)<>0)Then
' 将输入数字转换为字符串
Dim numStr As String
numStr = CStr(num)
' 查找小数点的位置
Dim dotPos As Integer
dotPos = InStr(numStr, ".")
' 提取整数部分的字符串
integerPartStr = Mid(numStr, 1, dotPos - 1)
' 提取小数部分的字符串
decimalPartStr = Mid(numStr, dotPos + 1)
' 确保小数部分为两位,不足两位补零
decimalPartStr = Mid(decimalPartStr & "00", 1, 2)
' 如果小数部分不全为零,则进行转换
If decimalPartStr <> "00" Then
digit = Val(Mid(decimalPartStr, 1, 1))
If digit <> 0 Then
decimalPart = decimalPart & numDict(digit) & "角"
End If
digit = Val(Mid(decimalPartStr, 2, 1))
If digit <> 0 Then
decimalPart = decimalPart & numDict(digit) & "分"
End If
End If
Else
' 如果输入是整数,直接将其转换为字符串作为整数部分
integerPartStr= CStr(num)
End If
''''''''''''''''''''''''''''''''''''''''''
' 处理整数部分
if Len(integerPartStr)>=20 then
MsgBox "数字太大,2024年中国GDP才134.91万亿元"
NumToChineseMoney="数字太大,2024年中国GDP才134.91万亿元"
Exit Function
End If
'把整数部分的字符串逆转,用循环加入单位
mystr=ReverseString(integerPartStr) '整数部分逆转后的字符串
integerPart=""
For i=0 to Len(mystr)-1 step 1
digit=val(Mid(mystr,i+1,1)) '这儿需要加1,否则bug
integerPart=numDict(digit) & unitDict(i) & integerPart
Next i
integerPart=Replace(integerPart,"零拾","零")
integerPart=Replace(integerPart,"零佰","零")
integerPart=Replace(integerPart,"零仟","零")
'把两个以上的零,合并成一个零
integerPart=Replace(integerPart,"零零零零","零")
integerPart=Replace(integerPart,"零零零","零")
integerPart=Replace(integerPart,"零零","零")
'这些单位,放在后面替换,特殊
integerPart=Replace(integerPart,"京零兆","京")
integerPart=Replace(integerPart,"京零亿","京")
integerPart=Replace(integerPart,"京零万","京")
integerPart=Replace(integerPart,"兆零亿","兆")
integerPart=Replace(integerPart,"兆零万","兆")
integerPart=Replace(integerPart,"亿零万","亿")
integerPart=Replace(integerPart,"零京","京")
integerPart=Replace(integerPart,"零兆","兆")
integerPart=Replace(integerPart,"零亿","亿")
integerPart=Replace(integerPart,"零万","万")
''''''''''''''''''''''''''''''''''''''''''
If integerPart = "" Then
' 如果整数部分为空,设为零
integerPart = "零"
End If
' 添加元
integerPart = integerPart & "元"
''''''''''''''''''''''''''''''''''''''''''
' 合并整数部分和小数部分
NumToChineseMoney = integerPart & decimalPart
If decimalPart = "" Then
' 如果没有小数部分,添加整字
NumToChineseMoney = NumToChineseMoney & "整"
End If
End Function
Sub TestNumToChineseMoney()
Dim number As Double
Dim result As String
' 示例 1:整数
number = 1000000001.23
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 2:带小数
number = 123.45
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 3:小数为零
number = 123.00
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 4:整数为零
number = 0.45
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
End Sub
优化了一下代码,如果不超过20位,应该没问题 nyy 发表于 2025-2-27 12:07
优化了一下代码,如果不超过20位,应该没问题
'逆转字符串功能,输入abcd,则返回dcba
Function ReverseString(ByVal str As String) As String
Dim i As Integer
Dim result As String
'默认结果初始值
result=""
'如果字符串的长度等于零,则返回空字符串
if Len(str)=0 then
ReverseString=str
Exit Function
end if
' 遍历原字符串,从最后一个字符开始
For i = Len(str) To 1 Step -1
' 将当前字符添加到结果字符串中
result = result & Mid(str, i, 1)
Next i
ReverseString = result
End Function
' 函数名称:NumToChineseMoney
' 功能:将输入的人民币数字金额转换为大写金额
' 输入参数:
' num:要转换的数字金额,可以是整数或小数类型
' 返回参数:
' 转换后的大写金额字符串
Function NumToChineseMoney(ByVal num As Variant) As String
' 存储数字 0 - 9 对应的大写汉字
Dim numDict(0 To 9) As String
' 存储个、十、百、千单位
Dim unitDict(0 To 19) As String
' 存储输入数字的整数部分的字符串形式
Dim integerPartStr As String
' 存储输入数字的小数部分的字符串形式
Dim decimalPartStr As String
' 存储转换后的整数部分大写金额
Dim integerPart As String
' 存储转换后的小数部分大写金额
Dim decimalPart As String
' 循环计数器,用于遍历数字字符串
Dim i As Integer
' 存储当前处理的数字
Dim digit As Integer
''''''''''''''''''''''''''''''''''''''''''
' 初始化数字对应的大写汉字
numDict(0) = "零"
numDict(1) = "壹"
numDict(2) = "贰"
numDict(3) = "叁"
numDict(4) = "肆"
numDict(5) = "伍"
numDict(6) = "陆"
numDict(7) = "柒"
numDict(8) = "捌"
numDict(9) = "玖"
'将来扩展用
' 空 拾 佰 仟 万 1万 = 10^{4}
' 拾 佰 仟 亿 1亿 = 10^{8}
' 拾 佰 仟 兆 1兆 = 10^{12}
' 拾 佰 仟 京 1京 = 10^{16}
' 拾 佰 仟 垓 1垓 = 10^{20}
' 拾 佰 仟 秭 1秭 = 10^{24}
' 拾 佰 仟 穰 1穰 = 10^{28}
' 拾 佰 仟 沟 1沟 = 10^{32}
' 拾 佰 仟 涧 1涧 = 10^{36}
' 拾 佰 仟 正 1正 = 10^{40}
' 拾 佰 仟 载 1载 = 10^{44}
'初始化个、十、百、千单位
unitDict(0)=""
unitDict(1)="拾"
unitDict(2)="佰"
unitDict(3)="仟"
unitDict(4)="万" '特殊
unitDict(5)="拾"
unitDict(6)="佰"
unitDict(7)="仟"
unitDict(8)="亿" '特殊
unitDict(9)="拾"
unitDict(10)="佰"
unitDict(11)="仟"
unitDict(12)="兆" '特殊
unitDict(13)="拾"
unitDict(14)="佰"
unitDict(15)="仟"
unitDict(16)="京" '特殊
unitDict(17)="拾"
unitDict(18)="佰"
unitDict(19)="仟"
''''''''''''''''''''''''''''''''''''''''''
' 处理小数部分
If (num-int(num)<>0)Then
' 将输入数字转换为字符串
Dim numStr As String
numStr = CStr(num)
' 查找小数点的位置
Dim dotPos As Integer
dotPos = InStr(numStr, ".")
' 提取整数部分的字符串
integerPartStr = Mid(numStr, 1, dotPos - 1)
' 提取小数部分的字符串
decimalPartStr = Mid(numStr, dotPos + 1)
' 确保小数部分为两位,不足两位补零
decimalPartStr = Mid(decimalPartStr & "00", 1, 2)
' 如果小数部分不全为零,则进行转换
If decimalPartStr <> "00" Then
digit = Val(Mid(decimalPartStr, 1, 1))
If digit <> 0 Then
decimalPart = decimalPart & numDict(digit) & "角"
End If
digit = Val(Mid(decimalPartStr, 2, 1))
If digit <> 0 Then
decimalPart = decimalPart & numDict(digit) & "分"
End If
End If
Else
' 如果输入是整数,直接将其转换为字符串作为整数部分
integerPartStr= CStr(num)
End If
''''''''''''''''''''''''''''''''''''''''''
' 处理整数部分
' 如果整数部分太大,则返回错误.
if Len(integerPartStr)>20 then
MsgBox "数字太大,2024年中国GDP才134.91万亿元(1.3491*10^14)"
NumToChineseMoney="数字太大,2024年中国GDP才134.91万亿元(1.3491*10^14)"
Exit Function
End If
'把整数部分的字符串逆转,用循环加入单位
mystr=ReverseString(integerPartStr) '整数部分逆转后的字符串
integerPart=""
For i=0 to Len(mystr)-1 step 1
digit=val(Mid(mystr,i+1,1)) '这儿需要加1,否则bug
integerPart=numDict(digit) & unitDict(i) & integerPart
Next i
integerPart=Replace(integerPart,"零拾","零")
integerPart=Replace(integerPart,"零佰","零")
integerPart=Replace(integerPart,"零仟","零")
'把两个以上的零,合并成一个零
integerPart=Replace(integerPart,"零零零零","零")
integerPart=Replace(integerPart,"零零零","零")
integerPart=Replace(integerPart,"零零","零")
'这些单位,放在后面替换,特殊
integerPart=Replace(integerPart,"京零兆","京")
integerPart=Replace(integerPart,"京零亿","京")
integerPart=Replace(integerPart,"京零万","京")
integerPart=Replace(integerPart,"兆零亿","兆")
integerPart=Replace(integerPart,"兆零万","兆")
integerPart=Replace(integerPart,"亿零万","亿")
integerPart=Replace(integerPart,"零京","京")
integerPart=Replace(integerPart,"零兆","兆")
integerPart=Replace(integerPart,"零亿","亿")
integerPart=Replace(integerPart,"零万","万")
''''''''''''''''''''''''''''''''''''''''''
If integerPart = "" Then
' 如果整数部分为空,设为零
integerPart = "零"
End If
' 添加元
integerPart = integerPart & "元"
'避免出现"京零元"这样的问题
integerPart=Replace(integerPart,"京零元","京元")
integerPart=Replace(integerPart,"兆零元","兆元")
integerPart=Replace(integerPart,"亿零元","亿元")
integerPart=Replace(integerPart,"万零元","万元")
''''''''''''''''''''''''''''''''''''''''''
' 合并整数部分和小数部分
NumToChineseMoney = integerPart & decimalPart
If decimalPart = "" Then
' 如果没有小数部分,添加整字
NumToChineseMoney = NumToChineseMoney & "整"
End If
End Function
Sub TestNumToChineseMoney()
Dim number As Double
Dim result As String
' 示例 1:整数
number = 1000000001.23
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 2:带小数
number = 123.45
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 3:小数为零
number = 123.00
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
' 示例 4:整数为零
number = 0.45
result = NumToChineseMoney(number)
MsgBox "数字 " & number & " 转换后的大写金额是:" & result
End Sub
继续优化代码,这样估计没bug了!
页:
[1]
2