|
楼主 |
发表于 2024-10-2 10:22
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
以下代码来自于百度ai,版权归于ai!
- Function NumberToWords(ByVal n As Long) As String
- Dim units() As String
- Dim lessThan20() As String
- Dim tens() As String
- Dim result As String
- Dim i As Long
- Dim billions As Long, millions As Long, thousands As Long
- Dim hundreds As Long, remainder As Long
-
- ' 定义英文数字单位
- lessThan20 = Split("Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen", ",")
- tens = Split(",Ten,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety", ",")
-
- If n = 0 Then
- NumberToWords = "Zero"
- Exit Function
- End If
-
- ' 处理大于19的数字
- If n < 0 Then
- NumberToWords = "Negative " & NumberToWords(-n)
- Exit Function
- End If
-
- billions = n \ 1000000000
- millions = (n Mod 1000000000) \ 1000000
- thousands = (n Mod 1000000) \ 1000
- hundreds = (n Mod 1000) \ 100
- remainder = n Mod 100
-
- If billions > 0 Then result = result & NumberToWords(billions) & " Billion "
- If millions > 0 Then result = result & NumberToWords(millions) & " Million "
- If thousands > 0 Then result = result & NumberToWords(thousands) & " Thousand "
- If hundreds > 0 Then result = result & NumberToWords(hundreds) & " Hundred "
-
- If remainder < 20 And remainder > 0 Then
- result = result & lessThan20(remainder)
- ElseIf remainder >= 20 Then
- result = result & tens(remainder \ 10)
- If remainder Mod 10 > 0 Then result = result & "-" & lessThan20(remainder Mod 10)
- End If
-
- ' 去除尾部多余的空格
- NumberToWords = Trim(result)
- End Function
复制代码 |
|