Public Function AmountInWords(ByVal nAmount As String, Optional ByVal wAmount _
As String = vbNullString, Optional ByVal nSet As Object = Nothing) As String
'Let's make sure entered value is numeric
If Not IsNumeric(nAmount) Then Return "Please enter numeric values only."
Dim tempDecValue As String = String.Empty : If InStr(nAmount, ".") Then _
tempDecValue = nAmount.Substring(nAmount.IndexOf("."))
nAmount = Replace(nAmount, tempDecValue, String.Empty)
Try
Dim intAmount As Long = nAmount
If intAmount > 0 Then
nSet = IIf((intAmount.ToString.Trim.Length / 3) _
> (CLng(intAmount.ToString.Trim.Length / 3)), _
CLng(intAmount.ToString.Trim.Length / 3) + 1, _
CLng(intAmount.ToString.Trim.Length / 3))
Dim eAmount As Long = Microsoft.VisualBasic.Left(intAmount.ToString.Trim, _
(intAmount.ToString.Trim.Length - ((nSet - 1) * 3)))
Dim multiplier As Long = 10 ^ (((nSet - 1) * 3))
Dim Ones() As String = _
{"", "One", "Two", "Three", _
"Four", "Five", _
"Six", "Seven", "Eight", "Nine"}
Dim Teens() As String = {"", _
"Eleven", "Twelve", "Thirteen", _
"Fourteen", "Fifteen", _
"Sixteen", "Seventeen", "Eighteen", "Nineteen"}
Dim Tens() As String = {"", "Ten", _
"Twenty", "Thirty", _
"Forty", "Fifty", "Sixty", _
"Seventy", "Eighty", "Ninety"}
Dim HMBT() As String = {"", "", _
"Thousand", "Million", _
"Billion", "Trillion", _
"Quadrillion", "Quintillion"}
intAmount = eAmount
Dim nHundred As Integer = intAmount \ 100 : intAmount = intAmount Mod 100
Dim nTen As Integer = intAmount \ 10 : intAmount = intAmount Mod 10
Dim nOne As Integer = intAmount \ 1
If nHundred > 0 Then wAmount = wAmount & _
Ones(nHundred) & " Hundred " 'This is for hundreds
If nTen > 0 Then 'This is for tens and teens
If nTen = 1 And nOne > 0 Then 'This is for teens
wAmount = wAmount & Teens(nOne) & " "
Else 'This is for tens, 10 to 90
wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, "-", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
Else 'This is for ones, 1 to 9
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
wAmount = wAmount & HMBT(nSet) & " "
wAmount = AmountInWords(CStr(CLng(nAmount) - _
(eAmount * multiplier)).Trim & tempDecValue, wAmount, nSet - 1)
Else
If Val(nAmount) = 0 Then nAmount = nAmount & _
tempDecValue : tempDecValue = String.Empty
If (Math.Round(Val(nAmount), 2) * 100) > 0 Then wAmount = _
Trim(AmountInWords(CStr(Math.Round(Val(nAmount), 2) * 100), _
wAmount.Trim & " Pesos And ", 1)) & " Cents"
End If
Catch ex As Exception
MessageBox.Show("Error Encountered: " & ex.Message, _
"Convert Numbers To Words", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return "!#ERROR_ENCOUNTERED"
End Try
'Trap null values
If IsNothing(wAmount) = True Then wAmount = String.Empty Else wAmount = _
IIf(InStr(wAmount.Trim.ToLower, "pesos"), _
wAmount.Trim, wAmount.Trim & " Pesos")
'Display the result
Return wAmount
End Function Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net. OneCompiler also has reference programs, where you can look for the sample code to get started with.
OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.
Public Module Program
Public Sub Main(args() As string)
Dim name as String = Console.ReadLine() ' Reading input from STDIN
Console.WriteLine("Hello " & name) ' Writing output to STDOUT
End Sub
End Module
Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.
Variable is a name given to the storage area in order to identify them in our programs.
Simple syntax of Variable declaration is as follows
Dim variableName [ As [ New ] dataType ] [ = initializer ]
variableName = value
If condition-expression Then
'code
End If
If(conditional-expression)Then
'code if the conditional-expression is true
Else
'code if the conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
Else If(conditional-expression) Then
'code if the above conditional-expression is true
Else
'code if the above conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
If(conditional-expression)Then
'code if the above conditional-expression is true
End If
End If
Select [ Case ] expression
[ Case expressionlist
'code ]
[ Case Else
'code ]
End Select
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Procedures are of two types.
Functions return a value when they are called.
[accessModifiers] Function functionName [(parameterList)] As returnType
'code
End Function
Sub-procedures are similar to functions but they don't return any value.
Sub ProcedureName (parameterList)
'Code
End Sub