View Single Post
 
Old 08-25-2015, 12:58 AM
Catty Catty is offline Windows 7 32bit Office 2010 32bit
Advanced Beginner
 
Join Date: Nov 2013
Posts: 39
Catty is on a distinguished road
Unhappy I need help in converting vb code to vba

Hi...

I need help in converting the below vb code to vba. I have been trying for a while now but i am unable to get the conversion to work 100%.


Quote:
Namespace numberstowords

Public Class NumbersToWords
Private Shared ReadOnly specialNames() As String = { "", " thousand,", " million,", " billion,", " trillion,", " quadrillion,", " quintillion," }

Private Shared ReadOnly tensNames() As String = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" }

Private Shared ReadOnly numNames() As String = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" }

Private Shared Function convertLessThanOneThousand(ByVal number As Integer) As String
Dim current As String

If number Mod 100 < 20 Then
current = numNames(number Mod 100)
number \= 100
Else
current = numNames(number Mod 10)
number \= 10

current = tensNames(number Mod 10) & current
number \= 10
End If
If number = 0 Then
Return current
End If
Return numNames(number) & " hundred and" & current
End Function

Public Shared Function convert(ByVal number As Integer) As String

If number = 0 Then
Return "zero"
End If

Dim prefix As String = ""

If number < 0 Then
number = -number
prefix = "negative"
End If

Dim current As String = ""
Dim place As Integer = 0

Do
Dim n As Integer = number Mod 1000
If n <> 0 Then
Dim s As String = convertLessThanOneThousand(n)
current = s & specialNames(place) & current
End If
place += 1
number \= 1000
Loop While number > 0

Return (prefix & current).Trim()
End Function
Public Shared Sub Main(ByVal args() As String)
Dim sc As New Scanner(System.in)
Console.WriteLine("Please enter the amount including R:")
Dim total As String = sc.nextLine()
total = total.Replace(" ", "")
Dim rands As String = total.Substring(1, total.LastIndexOf(",", StringComparison.Ordinal) - 1)
Dim cents As String = total & ""
cents = cents.Substring(cents.LastIndexOf(",", StringComparison.Ordinal)+1)

Dim convertedRands As String = convert(Integer.Parse(rands))
convertedRands = convertedRands & " rand and "
If convertedRands.Contains(", rand") Then
convertedRands = convertedRands.Replace(", rand", " rand")
End If
If convertedRands.Contains("hundred and thousand") Then
convertedRands = convertedRands.Replace("hundred and thousand", "hundred thousand")
End If
If convertedRands.Contains("and rand") AndAlso Not convertedRands.Contains("thousand") Then
convertedRands = convertedRands.Replace("and rand", "rand")
End If
If convertedRands.Contains("hundred and rand") Then
convertedRands = convertedRands.Replace("hundred and rand", "hundred rand")
End If
Dim convertedCents As String = convert(Integer.Parse(cents))

Console.WriteLine("Total = " & total)
Console.WriteLine("Rands = " & rands)
Console.WriteLine("Cents = " & cents)
Console.WriteLine("*** " & convertedRands & convertedCents.Replace(" ", "-") & " cents")
Console.WriteLine("*** " & convert(-55))
End Sub

End Class

End Namespace
Reply With Quote