## Introduction

A lot of programs and code on converting numbers to words can be found everywhere on the internet but I decided to make my own. I came up with my own ideas while I was working with an accounting application which contains a check writer module that needs to convert the entered value to words. And here now, I am sharing with everyone.

This program contains neat code on how to convert numbers to words. To minimize the lines of codes, I used a recursive function.

The logic is simple:

- Segregate the whole numbers in sets with 3-digit numbers each.
- Determine the ones, tens and hundreds of each digit in the sets and put the corresponding word for each.
- Determine what set should be the thousand, million, billion, until quintillion, then put the corresponding word.
- If decimal numbers exist, determine also the tenths and hundredths and put the corresponding word. (If greater than tenths, say thousandths (3-digit value) or millionths (4-digit value), the decimal value is rounded off to hundredths only, that's 2-digit decimal value.)
- Combine the evaluated statements and display the result.

## The Source Code

This is the complete code of the function:

Public Function AmountInWords(ByVal nAmount As String, Optional ByVal wAmount _
As String = vbNullString, Optional ByVal nSet As Object = Nothing) As String
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 "
If nTen > 0 Then
If nTen = 1 And nOne > 0 Then
wAmount = wAmount & Teens(nOne) & " "
Else
wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, "-", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
Else
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
If IsNothing(wAmount) = True Then wAmount = String.Empty Else wAmount = _
IIf(InStr(wAmount.Trim.ToLower, "pesos"), _
wAmount.Trim, wAmount.Trim & " Pesos")
Return wAmount
End Function

The zipped source code has detailed comments and remarks for easy understanding.

## Using the Code

The function is easy to use. Just put the function inside your form's code behind or in a module of your project and call it like this:

lblResult.Text = AmountInWords(txtAmount.Text.Trim)

**Important**: The parameter values you pass into the function should always be in trimmed `string `

format. (See **Points of Interest** subsection of this article for explanation.)

The value you entered will automatically be converted to words. To show you the result, here is the screenshot of the program:

**Note**: This version of conversion of numbers to words can convert the maximum value of the `LONG`

datatype which is "`9223372036854775807`

" (that's more than 9 quintillion) plus any 2-digit decimal value. If a 3-digit number is entered, the numbers are automatically rounded off to 2 digits.

## Points of Interest

Before I successfully completed the function, I had run into a little bit of problem. That problem is: when I used a number datatype, say `DOUBLE`

, `SINGLE`

, or `DECIMAL`

, or apply type-casting keywords to the entered value like `CDBL`

, `VAL`

, `CSNG`

, and others, and entered values that is more than the range of these datatypes, the program encounters an error and it does not show the correct result. For example:

Let's say I entered the value `42326432465566423`

. The following error occurred:

Another one that I encountered is this value: `282352345233221.66`

.

Can you see it? The decimal value is automatically rounded off to a whole number.

Well, the trick/solution that I found to this is to treat the entered value as `string`

. Do **not** convert or cast the value as number though it's actually a number itself. Retain it as `string`

. When you do, you get the exact result that you want.

That's all for this tip. I hope it helps!