- 注册时间
- 2021-11-19
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 9194
- 在线时间
- 小时
|
楼主 |
发表于 2025-2-27 11:34:55
|
显示全部楼层
本帖最后由 nyy 于 2025-2-27 11:53 编辑
- '逆转字符串功能,输入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 壹拾亿万零壹元整
这个结果显然错误。 |
|