怎么用VB2008的函数function 写阿拉伯数字转化成英文的 求VB大神 速度最快的追加50

如题所述

新加一个类,命名为Converter,Converter.vb的代码如下:
Public Class Converter
#Region " Constructor "
Public Sub New(ByVal amount As Long)
m_amount = amount
End Sub
#End Region

#Region " Public Methods"

Public Function Convert() As String

Dim convertedString As String = String.Empty

convertedString = Me.EnglishStyle
Return convertedString

End Function
#End Region

#Region " Private Methods"

#Region " English Style "

Private Function EnglishStyle()

If Amount = 0 Then Return "Zero" 'Unique and exceptional case
'Overflow exception will occur when 18+ digits are passed as
'Long can handle upto 18 digits but the routine below can convert upto 24 digits
Dim amountString As String = Amount.ToString

Dim result As String = String.Empty
Dim tempResult As String = String.Empty
Dim i As Integer = 3
Dim newAmount As String = amountString
Dim workedStringLength As Integer = 0
Dim concatHigherDigitString As Boolean

Do

concatHigherDigitString = False
If i > 3 Then concatHigherDigitString = True 'to suffix the Thousand/Million/Billion type of Word
If newAmount.Length >= 4 Then 'work with 3 right most digits at a time

newAmount = amountString.Substring(amountString.Length - i, 3)
End If
'do the conversion and affist the Thousand/Million/Billion type of word at the end when needed

If concatHigherDigitString AndAlso CInt(newAmount) <> 0 Then
result = ThreeDigitsConverter(CInt(newAmount)) & " " & HigherDigitEnglishStringArray(i / 3 + 1) & " " & result
Else
result = ThreeDigitsConverter(CInt(newAmount))
End If
workedStringLength += newAmount.Length

newAmount = amountString.Substring(0, amountString.Length - workedStringLength)
i += 3
Loop Until amountString.Length <= workedStringLength
Return RemoveSpaces(result)

End Function

Private Function ThreeDigitsConverter(ByVal amount As Integer) As String

Dim amountString As String = amount.ToString
'convert numbers to array of each digit

Dim amountArray(amountString.Length - 1) As Integer
For i As Integer = amountArray.Length To 1 Step -1
amountArray(i - 1) = amountString.Substring(i - 1, 1)
Next
Dim j As Integer = 0

Dim digit As Integer = 0
Dim result As String = String.Empty
Dim separator As String = String.Empty
Dim higherDigitEnglishString As String = String.Empty
Dim codeIndex As String = String.Empty
For i As Integer = amountArray.Length To 1 Step -1

j = amountArray.Length - i
digit = amountArray(j)
codeIndex = EnglishCodeArray(i - 1)

higherDigitEnglishString = HigherDigitEnglishStringArray(CInt(codeIndex.Substring(0, 1)) - 1)
If codeIndex = "1" Then 'Number [1 9]

result = result & separator & SingleDigitStringArray(digit)
ElseIf codeIndex.Length = 2 And digit <> 0 Then 'Number in tenth place and skip if digit is 0

If digit = 1 Then 'Number [Eleven, Twelve,...., Nineteen]

Dim suffixDigit As Integer = amountArray(j + 1)
result = result & separator & TenthDigitStringArray(suffixDigit) & " " & higherDigitEnglishString
i -= 1 'Skip the next round as we already looked at it
Else 'Number [tenth] and [20+]

result = result & separator & DoubleDigitsStringArray(digit) & " " & higherDigitEnglishString
End If
ElseIf digit <> 0 Then 'Standard Number like 100, 1000, 1000000 and skip if digit is 0

result = result & separator & SingleDigitStringArray(digit) & " " & higherDigitEnglishString
End If
separator = " "

Next
Return result

End Function
#End Region

Private Function RemoveSpaces(ByVal word As String) As String

Dim regEx As New System.Text.RegularExpressions.Regex(" ")
Return regEx.Replace(word, " ").Trim
End Function
#End Region

#Region " Property"

Public ReadOnly Property Amount As Long

Get
Return m_amount
End Get
End Property
#End Region
#Region " Fields"

Private m_amount As Long

Private SingleDigitStringArray() As String = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}

Private DoubleDigitsStringArray() As String = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}
Private TenthDigitStringArray() As String = {"Ten", "Eleven", "Tweleve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}
Private HigherDigitEnglishStringArray() As String = {"", "", "Hundred", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion"}

Private HigherDigitAsianStringArray() As String = {"", "", "Hundred", "Thousand", "Lakh", "Karod", "Arab", "Kharab"}
Private EnglishCodeArray() As String = {"1", "22", "3"}

#End Region

End Class

-------------------------------------------------------------------------------------------------------------
窗体代码如下:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

If TextBox1.Text = String.Empty Then
Label1.Text = String.Empty
Return
End If
Dim number As Long = CLng(Me.TextBox1.Text)

Dim converter As New Converter(number)

Me.Label1.Text = converter.Convert
End Sub
End Class追问

谢谢你的回答,但是和我想要得完全不是一个回答 

以下是我10以内的代码 我想知道10以上

 

 

 

 

 

           

 

 

 

 

 

 

追答

很明显,不能用select case语句,因为数字有几千万、几亿,你要写到什么时候?我的代码是完全正确的,无论多大的数字,都可以出正确的英文

追问

虽然看的不是很懂。。但是还是谢谢你 至少就你一个人回答我了 能给个QQ或者别的联系方式吗?我还有问题

追答

27 87 00294

温馨提示:内容为网友见解,仅供参考
无其他回答

怎么用VB2008的函数function 写阿拉伯数字转化成英文的 求VB大神 速度...
Public Class Converter Region " Constructor "Public Sub New(ByVal amount As Long)m_amount = amount End Sub End Region Region " Public Methods"Public Function Convert() As String Dim convertedString As String = String.Empty convertedString = Me.EnglishStyle Return convertedString End ...

用VB,通过一个命令按钮,将阿拉伯数字金额转化成汉字大写金额~~求大神...
Private Sub Command3_Click()Dim i As Integer, k As Integer, l As Integer, m As Integer, tmp As String, s1 As String, s2 As String, s3 As String, s4 As String tmp = Format(Trim(Text1.Text), "0.00")l = InStr(1, tmp, ".")k = Len(tmp)For i = 1 To k ' ...

...英文字母、数字]的组合,每五个加一个连字符如何写
Private Sub Form_Click()'使用chr()函数 '其中48~57为0到9十个阿拉伯数字 '65~90为26个大写英文字母 '97~122号为26个小写英文字母 'a = Int(Rnd * (da - xiao + 1) + xiao)X = ""For i = 1 To 20 s = Int(Rnd * (3))Select Case s Case 0 b = Int(Rnd * (57 -...

怎么用VBS写一个判断大小的程序?(谢谢您的回答)
在inputbox右边的括号里是参数列表, 每个参数用","分隔开, 每个参数有不同的功效, 比如第一个参数会显示在提示里, 我们把msg这个变量作为第一个参数传给了Inputbox 函数, 而msg="请输入你的名字:", 所以我们在对话框的提示栏就会看到"请输入你的名字:" 第二个参数是对话框的标题, 我们用直接量(学名叫"...

速达合计大写怎么设置不了
确认输入金额正确。在速达合计中,大写金额是根据输入的数字金额自动生成的,如果输入的金额不正确,大写金额也会出现错误。检查软件设置。有些速达合计软件可能需要在设置中开启大写金额功能才能正常使用。可以在软件设置中查找相关选项并进行设置。更新软件版本。如果你使用的是较老的速达合计版本,可能会存在...

相似回答
大家正在搜