新加一个类,命名为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