Click here to Skip to main content
15,882,329 members
Articles / Programming Languages / Visual Basic

Convert Numbers to Words Neatly

Rate me:
Please Sign up or sign in to vote.
4.71/5 (6 votes)
3 Feb 2013CPOL3 min read 80.8K   4.2K   13   15
This tip shows you how to convert numbers to words neatly.


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:

  1. Segregate the whole numbers in sets with 3-digit numbers each.
  2. Determine the ones, tens and hundreds of each digit in the sets and put the corresponding word for each.
  3. Determine what set should be the thousand, million, billion, until quintillion, then put the corresponding word.
  4. 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.)
  5. 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
    '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)

        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)
            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

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:

Image 1

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:

Image 2

Another one that I encountered is this value: 282352345233221.66.

Image 3

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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Philippines Philippines
I am a software developer for more than 5 years now. Programming is my passion.

Comments and Discussions

QuestionAbout the Dollar and cents Pin
Maroun Jahchan15-Mar-24 10:17
Maroun Jahchan15-Mar-24 10:17 
GeneralThanks Pin
Member 1290943924-Oct-22 5:49
Member 1290943924-Oct-22 5:49 
QuestionReading number line Pin
Eli Samuel26-Apr-19 19:36
Eli Samuel26-Apr-19 19:36 
QuestionGood day , Pin
Member 1392837127-Jul-18 6:07
Member 1392837127-Jul-18 6:07 
Question3 or more decimal digit not rounding Pin
Squiddles16-Jul-18 1:34
Squiddles16-Jul-18 1:34 
SuggestionTo be done (modifications for Europe or will not work) Pin
Member 43148592-May-18 3:01
Member 43148592-May-18 3:01 
Questionneed Zero Cents written when found .00 in value Pin
Member 1230162014-Feb-18 22:17
Member 1230162014-Feb-18 22:17 
QuestionHow to change Pesos to rupee Pin
RDPU28-Aug-16 2:47
RDPU28-Aug-16 2:47 
AnswerRe: How to change Pesos to rupee Pin
dinesh-solanki23-Apr-18 6:24
dinesh-solanki23-Apr-18 6:24 
AnswerRe: How to change Pesos to rupee Pin
VBK17-Dec-19 14:33
VBK17-Dec-19 14:33 
GeneralThanks Pin
Member 1061874312-Sep-15 23:49
Member 1061874312-Sep-15 23:49 
QuestionAdd Rupees Pin
Member 1122632327-Nov-14 19:47
Member 1122632327-Nov-14 19:47 
AnswerRe: Add Rupees Pin
dinesh-solanki23-Apr-18 6:19
dinesh-solanki23-Apr-18 6:19 
QuestionUse chars instead ? Pin
Robert Slaney5-Feb-13 22:43
Robert Slaney5-Feb-13 22:43 
GeneralMy vote of 5 Pin
_Vitor Garcia_5-Feb-13 6:25
_Vitor Garcia_5-Feb-13 6:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.