读书人

求数字转换成英文大写

发布时间: 2012-01-31 21:28:41 作者: rapoo

求数字转换成英文大写,在线等
求数字转换成英文大写,在线等

[解决办法]
1-> ONE
2-> TWO
?? 不大懂
[解决办法]
查看ASCII码表,转换时相应的加减其值
[解决办法]
dim i
for i=97 to 122
debug.print StrConv(Chr(i), vbProperCase);
next

这个?
[解决办法]
呼唤问题描述。
[解决办法]
Up
[解决办法]
不能只求现成的,简单的要自己多动动手
[解决办法]
求数据转人民币的代码:p
[解决办法]
18分太少。100分马上实现。呵呵。
"求数字转换成英文大写 ":请举个例,不要让大家猜。我们没有义务去帮你花时间猜。
[解决办法]
楼主的要求是把一个阿拉伯数字转换成英文,例如1234 -> one thousand two hundred and thirty four。
[解决办法]
要求读出小数点吗?仅仅是整数?
[解决办法]
大概是这样:

Public Function Number2Word(ByVal x As Integer) As String
Dim Ones(1 To 19) As String
Dim Tens(2 To 9) As String
Dim tmp As Integer
Dim myWords As String

If x > 999 Then Exit Function

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 "
Ones(10) = "TEN "
Ones(11) = "ELEVEN "
Ones(12) = "TWELVE "
Ones(13) = "THIRTEEN "
Ones(14) = "FOURTEEN "
Ones(15) = "FIFTEEN "
Ones(16) = "SIXTEEN "
Ones(17) = "SEVENTEEN "
Ones(12) = "EIGHTEEN "
Ones(19) = "NINETEEN "

Tens(2) = "TWENTY "
Tens(3) = "THIRTY "
Tens(4) = "FORTY "
Tens(5) = "FIFTY "
Tens(6) = "SIXTY "
Tens(7) = "SEVENTY "
Tens(8) = "EIGHTY "
Tens(9) = "NINETY "

tmp = x \ 100
If tmp Then
myWords = Ones(tmp) & " HUNDRED "
End If

tmp = x Mod 100
If tmp < 20 Then
myWords = IIf(myWords > " ", myWords & " AND ", " ") & Ones(tmp)
Else
tmp = tmp \ 10
myWords = IIf(myWords > " ", myWords & " AND ", " ") & Tens(tmp)
tmp = x Mod 10
If tmp Then myWords = IIf(myWords > " ", myWords & " ", " ") & Ones(tmp)
End If
Number2Word = myWords
End Function

Public Function BigNumber2Word(ByVal x As Long) As String
Dim tmp As Long
Dim myWords As String

If x > 999999999 Then Exit Function
tmp = x \ 1000000
If tmp Then
myWords = Number2Word(tmp) & " MILLION "
End If

tmp = x Mod 1000000
If tmp Then
tmp = tmp \ 1000
If tmp Then
myWords = IIf(myWords > " ", myWords & " ", " ") & Number2Word(tmp) & " THOUNSAND "


End If

tmp = x Mod 1000
If tmp Then
myWords = IIf(myWords > " ", myWords & " ", " ") & Number2Word(tmp)
End If
End If
BigNumber2Word = myWords
End Function

[解决办法]
Private Function NumberAsText(NumberIn As Variant, _
Optional AND_or_CHECK_or_DOLLAR As String) As String
Dim cnt As Long
Dim DecimalPoint As Long
Dim CardinalNumber As Long
Dim CommaAdjuster As Long
Dim TestValue As Long
Dim CurrValue As Currency
Dim CentsString As String
Dim NumberSign As String
Dim WholePart As String
Dim BigWholePart As String
Dim DecimalPart As String
Dim tmp As String
Dim sStyle As String
Dim bUseAnd As Boolean
Dim bUseCheck As Boolean
Dim bUseDollars As Boolean


'----------------------------------------
'Begin setting conditions for formatting
'----------------------------------------

'Determine whether to apply special formatting.
'If nothing passed, return routine result
'converted only into its numeric equivalents,
'with no additional format text.
sStyle = LCase(AND_or_CHECK_or_DOLLAR)

'User passed "AND ": "and " will be added
'between hundredths and tens of dollars,
'ie "Three Hundred and Forty Two "
bUseAnd = sStyle = "and "

'User passed "DOLLAR ": "dollar(s) " and "cents "
'appended to string,
'ie "Three Hundred and Forty Two Dollars "
bUseDollars = sStyle = "dollar "

'User passed "CHECK " *or* "DOLLAR "
'If "check ", cent amount returned as a fraction /100
'i.e. "Three Hundred Forty Two and 00/100 "
'If "dollar " was passed, "dollar(s) " and "cents "
'appended instead.
bUseCheck = (sStyle = "check ") Or (sStyle = "dollar ")


'----------------------------------------
'Check/create array. If this is the first
'time using this routine, create the text
'strings that will be used.
'----------------------------------------
If Not IsBounded(sNumberText) Then
Call BuildArray(sNumberText)
End If

'----------------------------------------
'Begin validating the number, and breaking
'into constituent parts
'----------------------------------------

'prepare to check for valid value in
NumberIn = Trim$(NumberIn)

If Not IsNumeric(NumberIn) Then

'invalid entry - abort
NumberAsText = "Error - Number improperly formed "
Exit Function

Else

'decimal check
DecimalPoint = InStr(NumberIn, ". ")

If DecimalPoint > 0 Then

'split the fractional and primary numbers
DecimalPart = Mid$(NumberIn, DecimalPoint + 1)
WholePart = Left$(NumberIn, DecimalPoint - 1)

Else

'assume the decimal is the last char
DecimalPoint = Len(NumberIn) + 1
WholePart = NumberIn

End If

If InStr(NumberIn, ",, ") Or _


InStr(NumberIn, ",. ") Or _
InStr(NumberIn, "., ") Or _
InStr(DecimalPart, ", ") Then

NumberAsText = "Error - Improper use of commas "
Exit Function

ElseIf InStr(NumberIn, ", ") Then

CommaAdjuster = 0
WholePart = " "

For cnt = DecimalPoint - 1 To 1 Step -1

If Not Mid$(NumberIn, cnt, 1) Like "[,] " Then

WholePart = Mid$(NumberIn, cnt, 1) & WholePart

Else

CommaAdjuster = CommaAdjuster + 1

If (DecimalPoint - cnt - CommaAdjuster) Mod 3 Then

NumberAsText = "Error - Improper use of commas "
Exit Function

End If 'If
End If 'If Not
Next 'For cnt
End If 'If InStr
End If 'If Not


If Left$(WholePart, 1) Like "[+-] " Then
NumberSign = IIf(Left$(WholePart, 1) = "- ", "Minus ", "Plus ")
WholePart = Mid$(WholePart, 2)
End If


'----------------------------------------
'Begin code to assure decimal portion of
'check value is not inadvertently rounded
'----------------------------------------
If bUseCheck = True Then

CurrValue = CCur(Val( ". " & DecimalPart))
DecimalPart = Mid$(Format$(CurrValue, "0.00 "), 3, 2)

If CurrValue > = 0.995 Then

If WholePart = String$(Len(WholePart), "9 ") Then

WholePart = "1 " & String$(Len(WholePart), "0 ")

Else

For cnt = Len(WholePart) To 1 Step -1

If Mid$(WholePart, cnt, 1) = "9 " Then
Mid$(WholePart, cnt, 1) = "0 "
Else
Mid$(WholePart, cnt, 1) = CStr(Val(Mid$(WholePart, cnt, 1)) + 1)
Exit For
End If

Next

End If 'If WholePart
End If 'If CurrValue
End If 'If bUseCheck

'----------------------------------------
'Final prep step - this assures number
'within range of formatting code below
'----------------------------------------
If Len(WholePart) > 9 Then
BigWholePart = Left$(WholePart, Len(WholePart) - 9)
WholePart = Right$(WholePart, 9)
End If

If Len(BigWholePart) > 9 Then

NumberAsText = "Error - Number too large "
Exit Function

ElseIf Not WholePart Like String$(Len(WholePart), "# ") Or _
(Not BigWholePart Like String$(Len(BigWholePart), "# ") _
And Len(BigWholePart) > 0) Then

NumberAsText = "Error - Number improperly formed "
Exit Function

End If

'----------------------------------------
'Begin creating the output string
'----------------------------------------

'Very Large values
TestValue = Val(BigWholePart)

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = HundredsTensUnits(CardinalNumber) & "Quadrillion "


TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Trillion "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
tmp = tmp & HundredsTensUnits(TestValue) & "Billion "
End If

'Lesser values
TestValue = Val(WholePart)

If TestValue = 0 And BigWholePart = " " Then tmp = "Zero "

If TestValue > 999999 Then
CardinalNumber = TestValue \ 1000000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Million "
TestValue = TestValue - (CardinalNumber * 1000000)
End If

If TestValue > 999 Then
CardinalNumber = TestValue \ 1000
tmp = tmp & HundredsTensUnits(CardinalNumber) & "Thousand "
TestValue = TestValue - (CardinalNumber * 1000)
End If

If TestValue > 0 Then
If Val(WholePart) < 99 And BigWholePart = " " Then bUseAnd = False
tmp = tmp & HundredsTensUnits(TestValue, bUseAnd)
End If

'If in dollar mode, assure the text is the correct plurality
If bUseDollars = True Then

CentsString = HundredsTensUnits(DecimalPart)

If tmp = "One " Then
tmp = tmp & "Dollar "
Else
tmp = tmp & "Dollars "
End If

If Len(CentsString) > 0 Then

tmp = tmp & " and " & CentsString

If CentsString = "One " Then
tmp = tmp & "Cent "
Else
tmp = tmp & "Cents "
End If

End If

ElseIf bUseCheck = True Then

tmp = tmp & "and " & Left$(DecimalPart & "00 ", 2)
tmp = tmp & "/100 "

Else

If Len(DecimalPart) > 0 Then

tmp = tmp & "Point "

For cnt = 1 To Len(DecimalPart)
tmp = tmp & " " & sNumberText(Mid$(DecimalPart, cnt, 1))
Next

End If 'If DecimalPart
End If 'If bUseDollars


'done!
NumberAsText = NumberSign & tmp

End Function

[解决办法]
这段带码很Flexible,可以读数字(number)或Amount of Currency in English.
good Luck!
[解决办法]
李开复说:
“算法就是力量;算法就是一切。”

to 2004kingbear() :
你自己测试一下吧。
[解决办法]
要求整数和小数都可以转换,代码简单易懂

整数和小数都可以转换,yeah!

你Test吗?在叫什么?!!!再叫打你头啦。呵呵。
[解决办法]
晕 有那么麻烦么!!
replace 转换函数不就可以咯!!
请LZ 和我做例题

exe内包含 2个 控件 text1 *1 command1*1

代码:
Private Sub Command1_Click()
Text1 = Replace(Text1, "1 ", "One ")
Text1 = Replace(Text1, "2 ", "Two ")
Text1 = Replace(Text1, "3 ", "three ")
Text1 = Replace(Text1, "4 ", "four ")


Text1 = Replace(Text1, "5 ", "file ")
Text1 = Replace(Text1, "6 ", "six ")
Text1 = Replace(Text1, "7 ", "seven ")
Text1 = Replace(Text1, "8 ", "eight ")
Text1 = Replace(Text1, "9 ", "nine ")
Text1 = Replace(Text1, "0 ", "zero ")
end sub

程序效果 在text1内输入的数字 全部变成英文 现在明白了吧! 可以根据自己需要修改上面底码[不知道英文写对了没...] 比如LZ 说的 数字转英文大写 改上面英文就OK了

给分!!给分!!

*******************************
cike技术交流群 16874787



[解决办法]

你只有一个山峰(金字塔 or 三角形),信 誉 值倒降到74分。

Reply off line.不再回答你的贴了。

[解决办法]
代码就是个例子,临时信手写的,提供一个思路。有特殊要求自己改改吧。

如果自己连脑子都不想动,也就没什么编程前途可言了。
[解决办法]
是阿,脑子都给楼上的动去了,楼主确实不用再动了

读书人网 >VB

热点推荐