求个数字转换成英文的函数

dut 2004-10-22 06:53:45
比如:
1234 one thousand two hundred thirty four

反正要符合英文读数字的习惯的(本人菜鸟,好多习惯还不清楚,如中间有0怎么读,小数点后到底怎么读,那些地方应该加and都不是很清楚)。

请熟悉的帮忙,我需要转换的数字是带小数点的

...全文
327 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
dut 2004-10-25
  • 打赏
  • 举报
回复
搞定

主要采用 leolan(史留香) 的方法
lxcc 2004-10-22
  • 打赏
  • 举报
回复
对了,看了版规,这样给分不违规吧?根据其他版经验是没问题的,如果有问题,请版主发消息给我,菜鸟最老实了。
//应该不违规吧! :P
dut 2004-10-22
  • 打赏
  • 举报
回复
对了,看了版规,这样给分不违规吧?根据其他版经验是没问题的,如果有问题,请版主发消息给我,菜鸟最老实了。

dut 2004-10-22
  • 打赏
  • 举报
回复
ok,今天收获还是不小的,
谢谢leolan(史留香) ,请到
http://community.csdn.net/Expert/topic/3483/3483118.xml?temp=.1927606
谢谢lxcc(虫子|专注于抢分) 和northwolves(狼行天下) ,请到
http://community.csdn.net/Expert/topic/3483/3483122.xml?temp=.3471796

菜鸟别的没有,可用分还是很多的。

大家帮我,现在的主要问题是小数和负数怎么表示。
lxcc 2004-10-22
  • 打赏
  • 举报
回复
负数怎么念我都不知道 :P
dut 2004-10-22
  • 打赏
  • 举报
回复
谢谢northwolves(狼行天下) ,对小数又提出一个解法,如
1234567.811
One million two hundred and thirty four thousand five hundred and sixty seven point eight one one
可惜整数有问题
1234567
One hundred and twenty three thousand four hundred and fifty six

还有,负数到底怎么办的呀!
northwolves 2004-10-22
  • 打赏
  • 举报
回复
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
dut 2004-10-22
  • 打赏
  • 举报
回复
谢谢楼上
100010001.11
结果:
ONE HUNDRED MILLION TEN THOUSAND ONE AND CENTS ELEVEN ONLY
小数部分的表示没个都不一样?

lxcc 2004-10-22
  • 打赏
  • 举报
回复
参考:http://www.soft2.com/softsc/zj/36583658.html
不一定有用! :P,仅供参考!

这个比较麻烦的说!
dut 2004-10-22
  • 打赏
  • 举报
回复
谢谢 leolan(史留香) 的答案

但是
123456111
结果是:

One hundred twenty-three million, four hundred fifty-six thousand, one hundred eleven and 00/100
有几个问题:
1、小数部分应该如何解决,这是最大的问题,现在用“XX/100”这种表示是不是合理的?
我把
'Get fractional part
strBuff = "and " & Format((dblVal - Int(dblVal)) * 100, "00") & "/100"
改成
'Get fractional part
If dblVal - Int(dblVal) > 0 Then
strBuff = "and " & Format((dblVal - Int(dblVal)) * 100, "00") & "/100"
Else
strBuff = ""
End If
正整数应该没有问题了
2、忘了说的还有个大问题,负数怎么表示?

大家帮忙!

starsoulxp 2004-10-22
  • 打赏
  • 举报
回复
关注
熊孩子开学喽 2004-10-22
  • 打赏
  • 举报
回复
英语不是一种符合逻辑的语言,用程序只能列举,从楼上的例子就可以看出来,主要是因为充满了不规则的变化。
leolan 2004-10-22
  • 打赏
  • 举报
回复
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 1's, 10's or 100's column
Select Case (nCol Mod 3)
Case 1 '1's 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, don't 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 '10's 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 '100's 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
dut 2004-10-22
  • 打赏
  • 举报
回复
楼上:
帮着写个函数!!
Andy__Huang 2004-10-22
  • 打赏
  • 举报
回复
1234 one thousand two hundred and thirty four

1234.5 one thousand two hundred and thirty four dot five

7,763

社区成员

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

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