2015-05-04 38 views
3

我想了許多從RDLC報告轉換爲一個字:需要幫助轉換一個數字的話

Public Shared Function changeToWords(ByVal numb As [String]) As [String] 
    Dim val As [String] = "" 
    Dim wholeNo As [String] = numb 
    Dim points As [String] = "" 
    Dim andStr As [String] = "" 
    Dim pointStr As [String] = "" 
    Dim endStr As [String] = "" 
    Try 
     Dim decimalPlace As Integer = numb.IndexOf(".") 
     If decimalPlace > 0 Then 
      wholeNo = numb.Substring(0, decimalPlace) 
      points = numb.Substring(decimalPlace + 1) 
      If Convert.ToInt32(points) > 0 Then 
       andStr = "point" 
       ' just to separate whole numbers from points 
       pointStr = translateCents(points) 
      End If 
     End If 

     val = [String].Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr) 
    Catch 
    End Try 
    Return val 
End Function 
Private Shared Function translateWholeNumber(ByVal number As [String]) As [String] 
    Dim word As String = "" 
    Try 
     Dim beginsZero As Boolean = False 
     'tests for 0XX 
     Dim isDone As Boolean = False 
     'test if already translated 
     Dim dblAmt As Double = (Convert.ToDouble(number)) 
     'if ((dblAmt > 0) && number.StartsWith("0")) 
     If dblAmt > 0 Then 
      'test for zero or digit zero in a nuemric 
      beginsZero = number.StartsWith("0") 

      Dim numDigits As Integer = number.Length 
      Dim pos As Integer = 0 
      'store digit grouping 
      Dim place As [String] = "" 
      'digit grouping name:hundres,thousand,etc... 
      Select Case numDigits 
       Case 1 
        'ones' range 
        word = ones(number) 
        isDone = True 
        Exit Select 
        ' TODO: might not be correct. Was : Exit Select 
       Case 2 
        'tens' range 
        word = tens(number) 
        isDone = True 
        Exit Select 
        ' TODO: might not be correct. Was : Exit Select 
       Case 3 
        'hundreds' range 
        pos = (numDigits Mod 3) + 1 
        place = " Hundred " 
        Exit Select 
        ' TODO: might not be correct. Was : Exit Select 
        'thousands' range 
       Case 4, 5, 6 
        pos = (numDigits Mod 4) + 1 
        place = " Thousand " 
        Exit Select 
        ' TODO: might not be correct. Was : Exit Select 
        'millions' range 
       Case 7, 8, 9 
        pos = (numDigits Mod 7) + 1 
        place = " Million " 
        Exit Select 
        ' TODO: might not be correct. Was : Exit Select 
       Case 10 
        'Billions's range 
        pos = (numDigits Mod 10) + 1 
        place = " Billion " 
        Exit Select 
       Case Else 
        ' TODO: might not be correct. Was : Exit Select 
        'add extra case options for anything above Billion... 
        isDone = True 
        Exit Select 
        ' TODO: might not be correct. Was : Exit Select 
      End Select 
      If Not isDone Then 
       'if transalation is not done, continue...(Recursion comes in now!!) 
       word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos)) 
       'check for trailing zeros 
       If beginsZero Then 
        word = " and " & word.Trim() 
       End If 
      End If 
      'ignore digit grouping names 
      If word.Trim().Equals(place.Trim()) Then 
       word = "" 
      End If 
     End If 

    Catch 
    End Try 
    Return word.Trim() 
End Function 
Private Shared Function tens(ByVal digit As [String]) As [String] 
    Dim digt As Integer = Convert.ToInt32(digit) 
    Dim name As [String] = Nothing 
    Select Case digt 
     Case 10 
      name = "Ten" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select  \ 
     Case 11 
      name = "Eleven" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 12 
      name = "Twelve" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 13 
      name = "Thirteen" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 14 
      name = "Fourteen" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 15 
      name = "Fifteen" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 16 
      name = "Sixteen" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 17 
      name = "Seventeen" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 18 
      name = "Eighteen" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 19 
      name = "Nineteen" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 20 
      name = "Twenty" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 30 
      name = "Thirty" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 40 
      name = "Fourty" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 50 
      name = "Fifty" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 60 
      name = "Sixty" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 70 
      name = "Seventy" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 80 
      name = "Eighty" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 90 
      name = "Ninety" 
      Exit Select 
     Case Else 
      ' TODO: might not be correct. Was : Exit Select 
      If digt > 0 Then 
       name = (Convert.ToString(tens(digit.Substring(0, 1) & "0")) & " ") & Convert.ToString(ones(digit.Substring(1))) 
      End If 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
    End Select 
    Return name 
End Function 
Private Shared Function ones(ByVal digit As [String]) As [String] 
    Dim digt As Integer = Convert.ToInt32(digit) 
    Dim name As [String] = "" 
    Select Case digt 
     Case 1 
      name = "One" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 2 
      name = "Two" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 3 
      name = "Three" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 4 
      name = "Four" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 5 
      name = "Five" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 6 
      name = "Six" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 7 
      name = "Seven" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 8 
      name = "Eight" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
     Case 9 
      name = "Nine" 
      Exit Select 
      ' TODO: might not be correct. Was : Exit Select 
    End Select 
    Return name 
End Function 
Private Shared Function translateCents(ByVal cents As [String]) As [String] 
    Dim cts As [String] = "" 
    Dim digit As [String] = "" 
    Dim engOne As [String] = "" 
    For i As Integer = 0 To cents.Length - 1 
     digit = cents(i).ToString() 
     If digit.Equals("0") Then 
      engOne = "Zero" 
     Else 
      engOne = ones(digit) 
     End If 
     cts += " " & engOne 
    Next 
    Return cts 
End Function 

我正在從轉換輸出的錯誤。

對於52001給定的輸出是Fifty Two Thousand and Hundred One。 但是,它應該是Fifty Two Thousand and One

+0

您可以舉例說明何時需要'和'。不是以英語爲母語的人,並且找不到示例。 – Jordumus

+0

請參閱http://codereview.stackexchange.com/questions/58833/number-to-words – dbasnett

+0

該代碼沒有解決方案 –

回答

0

當子字符串的開頭爲零時,您需要添加一個條件語句來修改字符串連接行爲。

'if transalation is not done, continue...(Recursion comes in now!!) 
If (number.Substring(0, 1) = "0") Then 
    word = translateWholeNumber(number.Substring(pos)) 
Else 
    word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos)) 
End If 

編輯:做的,如果發送到changeToWords字符串任何零點開始,這打破輸出。要糾正這種情況,您可以在處理字符串之前修剪前導零: