类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
比如:
1234 one thousand two hundred thirty four
反正要符合英文读数字的习惯的(本人菜鸟,好多习惯还不清楚,如中间有0怎么读,小数点后到底怎么读,那些地方应该加and都不是很清楚)。
请熟悉的帮忙,我需要转换的数字是带小数点的
网友回答:
1234 one thousand two hundred and thirty four
1234.5 one thousand two hundred and thirty four dot five
Private Function NumToText(dblVal As Double) As String
Static Ones(0 To 9) As String
Static Teens(0 To 9) As String
Static Tens(0 To 9) As String
Static Thousands(0 To 4) As String
Static bInit As Boolean
Dim i As Integer, bAllZeros As Boolean, bShowThousands As Boolean
Dim strVal As String, strBuff As String, strTemp As String
Dim nCol As Integer, nChar As Integer
Only handles positive values
Debug.Assert dblVal >= 0
If bInit = False Then
Initialize array
bInit = True
Ones(0) = "zero"
Ones(1) = "one"
Ones(2) = "two"
Ones(3) = "three"
Ones(4) = "four"
Ones(5) = "five"
Ones(6) = "six"
Ones(7) = "seven"
Ones(8) = "eight"
Ones(9) = "nine"
Teens(0) = "ten"
Teens(1) = "eleven"
Teens(2) = "twelve"
Teens(3) = "thirteen"
Teens(4) = "fourteen"
Teens(5) = "fifteen"
Teens(6) = "sixteen"
Teens(7) = "seventeen"
Teens(8) = "eighteen"
Teens(9) = "nineteen"
Tens(0) = ""
Tens(1) = "ten"
Tens(2) = "twenty"
Tens(3) = "thirty"
Tens(4) = "forty"
Tens(5) = "fifty"
Tens(6) = "sixty"
Tens(7) = "seventy"
Tens(8) = "eighty"
Tens(9) = "ninety"
Thousands(0) = ""
Thousands(1) = "thousand" US numbering
Thousands(2) = "million"
Thousands(3) = "billion"
Thousands(4) = "trillion"
End If
Trap errors
On Error GoTo NumToTextError
Get fractional part
strBuff = "and " & Format((dblVal - Int(dblVal)) * 100, "00") & "/100"
Convert rest to string and process each digit
strVal = CStr(Int(dblVal))
Non-zero digit not yet encountered
bAllZeros = True
Iterate through string
For i = Len(strVal) To 1 Step -1
Get value of this digit
nChar = Val(Mid$(strVal, i, 1))
Get column position
nCol = (Len(strVal) - i) + 1
Action depends on 1s, 10s or 100s column
Select Case (nCol Mod 3)
Case 1 1s position
bShowThousands = True
If i = 1 Then
First digit in number (last in loop)
strTemp = Ones(nChar) & " "
ElseIf Mid$(strVal, i - 1, 1) = "1" Then
This digit is part of "teen" number
strTemp = Teens(nChar) & " "
i = i - 1 Skip tens position
ElseIf nChar > 0 Then
Any non-zero digit
strTemp = Ones(nChar) & " "
Else
This digit is zero. If digit in tens and hundreds column
are also zero, dont show "thousands"
bShowThousands = False
Test for non-zero digit in this grouping
If Mid$(strVal, i - 1, 1) <> "0" Then
bShowThousands = True
ElseIf i > 2 Then
If Mid$(strVal, i - 2, 1) <> "0" Then
bShowThousands = True
End If
End If
strTemp = ""
End If
Show "thousands" if non-zero in grouping
If bShowThousands Then
If nCol > 1 Then
strTemp = strTemp & Thousands(nCol \ 3)
If bAllZeros Then
strTemp = strTemp & " "
Else
strTemp = strTemp & ", "
End If
End If
Indicate non-zero digit encountered
bAllZeros = False
End If
strBuff = strTemp & strBuff
Case 2 10s position
If nChar > 0 Then
If Mid$(strVal, i + 1, 1) <> "0" Then
strBuff = Tens(nChar) & "-" & strBuff
Else
strBuff = Tens(nChar) & " " & strBuff
End If
End If
Case 0 100s position
If nChar > 0 Then
strBuff = Ones(nChar) & " hundred " & strBuff
End If
End Select
Next i
Convert first letter to upper case
strBuff = UCase$(Left$(strBuff, 1)) & Mid$(strBuff, 2)
EndNumToText:
Return result
NumToText = strBuff
Exit Function
NumToTextError:
strBuff = "#Error#"
Resume EndNumToText
End Function
英语不是一种符合逻辑的语言,用程序只能列举,从楼上的例子就可以看出来,主要是因为充满了不规则的变化。
关注
参考:http://www.soft2.com/softsc/zj/36583658.html
不一定有用! :P,仅供参考!
这个比较麻烦的说!
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 1s, 10s or 100s column
Select Case (nCol Mod 3)
Case 1 1s 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 10s position
If nChar > 0 Then buff = IIf(Mid$(N, i + 1, 1) <> "0", tens(nChar) & buff, tens(nChar) & buff)
Case 0 100s 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