Click here to Skip to main content
13,198,068 members (81,861 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 3 Feb 2013

Convert Numbers to Words Neatly

, 3 Feb 2013
Rate this:
Please Sign up or sign in to vote.
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:

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!


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


About the Author

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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
VitorHugoGarcia5-Feb-13 6:25
memberVitorHugoGarcia5-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 4 Feb 2013
Article Copyright 2013 by Normz Antonino
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid