急需将数字转换成英文?

machunfeng 2003-02-25 10:16:34
我是VB菜鸟级人物,现急需将数字转换成英文的函数原码或者dll的原码,完成的功能为,如235,输出结果应为TWO HUNRED AND THIRTY-FIVE.希望给我的应能处理多几位数字,如123456等
...全文
118 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
山顶洞人-平 2003-02-25
  • 打赏
  • 举报
回复
你可以先判断数字的位数,然后取出数字的每一位,转换为相应的英文,最后对各种不同位数的数字套用一个各自的模式
northwolves 2003-02-25
  • 打赏
  • 举报
回复
下面是经我修改过的一段代码:

Private Sub Command1_Click()
MsgBox numtotext("37456432503.22")
End Sub

Function numtotext(ByVal numstr As String) As String

Dim ones, teens, tens, thousands
Dim i As Long, p As Long, nCol As Long, kilo As Boolean
Dim buff As String, temp As String, nChar As String, N As String

ones = Array(" zero ", " one ", " two ", " three ", " four ", " five ", " six ", " seven ", " eight ", " nine ")
teens = Array(" ten ", " eleven ", " twelve ", " thirteen ", " fourteen ", " fifteen ", " sixteen ", " seventeen ", " eighteen ", " nineteen ")
tens = Array("", " ten ", " twenty ", " thirty ", " forty ", " fifty ", " sixty ", " seventy ", " eighty ", " ninety ")
thousands = Array("", " thousand ", " million ", " billion ", " trillion ")
buff = ""

If numstr = "" Then MsgBox "数字为空!!!" & vbCrLf & vbCrLf & "No Number Exists!!!", 64, "警告": Exit Function
If IsNumeric(numstr) = False Then MsgBox "非数字!!!" & vbCrLf & vbCrLf & "Not a Number!!!", 64, "警告": Exit Function
p = IIf(InStr(1, numstr, ".") > 0, InStr(1, numstr, "."), Len(numstr))
If p >= 16 Then MsgBox "转换的数字不得大于一千万亿!!!" & vbCrLf & vbCrLf & "The Number To Be Converted Must Less Than One Thousand Trillion!!!", 64, "警告": Exit Function
N = Left(numstr, p - 1)

For i = p + 1 To Len(numstr)
buff = buff & ones((Mid(numstr, i, 1)))

Next
buff = IIf(buff = "", "", " point " & buff)
For i = Len(N) To 1 Step -1 'Get value of this digit
nChar = Mid(N, i, 1) 'Get column position
nCol = (Len(N) - i) + 1 'Action depends on 1's, 10's or 100's column
Select Case (nCol Mod 3)

Case 1 '1's position
kilo = True
If i = 1 Then
temp = ones(nChar) 'First digit in number (last in loop)

ElseIf Mid(N, i - 1, 1) = "1" Then
temp = teens(nChar): 'This digit is part of "teen" number
i = i - 1 'Skip tens position

ElseIf nChar > 0 Then
temp = ones(nChar) 'Any non-zero digit
Else
kilo = False
'Test for non-zero digit in this grouping
If Mid(N, i - 1, 1) <> "0" Then
kilo = True
ElseIf i > 2 Then
If Mid$(N, i - 2, 1) <> "0" Then kilo = True
temp = ""
End If
End If
'Show "thousands" if non-zero in grouping
If kilo Then buff = temp & IIf(nCol > 1, thousands(nCol \ 3), "") & buff
Case 2 '10's position
If nChar > 0 Then buff = IIf(Mid$(N, i + 1, 1) <> "0", tens(nChar) & "-" & buff, tens(nChar) & buff)

Case 0 '100's position
buff = Switch(nChar > 0, ones(nChar) & " hundred and ", nChar = 0 And nCol <> Len(N), " and ") & buff
End Select
Next i
Do While InStr(1, buff, " and and ") > 0
buff = Replace(buff, " and and ", " and ")
Loop
For i = 1 To 4
buff = Replace(buff, " and " & thousands(i), thousands(i))
Next
buff = Replace(buff, " and point ", " point ")
buff = Replace(buff, " ", " ")
buff = IIf(Right(buff, 4) = "and ", Left(buff, Len(buff) - 4), buff)
buff = UCase(Left(buff, 2)) & Mid(buff, 3, Len(buff) - 2) 'Convert first letter to upper case

numtotext = buff 'Return result
End Function

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧