Number to Words Converter

Recently I had a requirement to convert number to words for a Cheque printing program. I searched the web for some ready-made handy function that would do this for me. But all the solutions I found were too long and almost all of them spanned into various functions and classes. That looked quite ugly, untidy and noobish too! There had to be a simpler way. And then I came up with my own version.

This function was primarily written for Excel VBA, but will work equally good for all versions of VBA, VB6, and VB.NET.

Function ToWords(ByVal number As Long) As String
    Dim major As Long, minor As Long, magnitude As Long, magnitudeW As String, length As Integer
    Select Case number
        Case 0
            ToWords = "Zero"
        Case 1 To 19
            ToWords = CStr(Choose(number, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"))
        Case 20, 30, 40, 50, 60, 70, 80, 90
            ToWords = CStr(Choose(number \ 10, "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"))
        Case Else
            length = Len(CStr(number))
            magnitudeW = CStr(Choose(length, "", "", "Hundred", "Thousand", "Thousand", "Lakh", "Lakh", "Crore", "Crore", "Arab", "Arab"))
            magnitude = CLng(Choose(length, 1, 10, 100, 1000, 1000, 100000, 100000, 10000000, 10000000, 1000000000, 1000000000))
            major = number \ magnitude
            minor = number - (major * magnitude)
            If number <= 99 Then major = major * 10
            ToWords = Trim(ToWords(major) & " " & magnitudeW) & CStr(IIf(minor = 0, "", " " & ToWords(minor)))
    End Select
End Function

The above code is for Indian Numbering System.

To customize it for the American/Arabic Numbering System (or other systems), you just need to change the following two lines:

'Set what each position is called in words. 
'We leave the ones and tens place empty because they are not called by any particular names, and already handled in other CASE blocks
magnitudeW = CStr(Choose(length, "", "", "Hundred", "Thousand", "Thousand", "Lakh", "Lakh", "Crore", "Crore", "Arab", "Arab"))

'Set the numerical values of each magnitudeW
magnitude = CLng(Choose(length, 1, 10, 100, 1000, 1000, 100000, 100000, 10000000, 10000000, 1000000000, 1000000000))

Set the magnitudeW to what word they are called as. (e.g. hundred, thousand, million etc.). And then set the magnitude to their equivalent number. We leave the ones and tens place empty because they are not called by any common name, and already handled in other CASE blocks.

So the final code for American/Arabic Numbering System would look like this:

Function ToWords(ByVal number As Long) As String
    Dim major As Long, minor As Long, magnitude As Long, magnitudeW As String, length As Integer
    Select Case number
        Case 0
            ToWords = "Zero"
        Case 1 To 19
            ToWords = CStr(Choose(number, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"))
        Case 20, 30, 40, 50, 60, 70, 80, 90
            ToWords = CStr(Choose(number \ 10, "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"))
        Case Else
            length = Len(CStr(number))
            magnitudeW = CStr(Choose(length, "", "", "Hundred", "Thousand", "Thousand", "Thousand", "Million", "Million", "Million", "Billion", "Billion", "Billion"))
            magnitude = CLng(Choose(length, 1, 10, 100, 1000, 1000, 1000, 1000000, 1000000, 1000000, 1000000000, 1000000000, 1000000000))
            major = number \ magnitude
            minor = number - (major * magnitude)
            If number <= 99 Then major = major * 10
            ToWords = Trim(ToWords(major) & " " & magnitudeW) & CStr(IIf(minor = 0, "", " " & ToWords(minor)))
    End Select
End Function

It is simple, easy to understand and less than 20 lines of code! That’s cool, isn’t it? Smile

Advertisements
Posted in VB.NET, VB6, VBA. 2 Comments »

CheckBox in Header Cell of DataGridView Control

Normally the DataGridView control does not support adding a checkbox in the header cell.

But this doesn’t mean that it is impossible. You can create your own class that inherits from the DataGridViewColumnHeaderCell and use it.

Here’s one class I had created for myself when I needed it. Besides putting the CheckBox in the header cell, it also has a couple of properties and methods to make it behave the way we are used to seeing in normal windows applications. You can modify it as per your needs. … Read the full article »

Undo/Redo Capable TextBox (winforms)

This is a continuation of my previous articles:

If you haven’t read the first part, I recommend you read that before continuing further, since we will be using the class we built there to our use in this demo.

In the first part we saw how to construct a class that can support undo/redo capabilities. In this article we will demonstrate its use by using a textbox. The normal windows forms TextBox has only one level of undo/redo capability which is no good for most situations. So let’s see what it would take to add proper undo/redo capabilities to the TextBox control.

The links to full source code and a demo executable can be found at the end of this article. … Read the full article »

Capturing the Deleted Text in TextBox when Backspace or Delete Key is Pressed

If you have ever wanted to get the deleted character/text from a textbox when the Backspace key or the Delete key on the keyboard is pressed, you would know that there is no easy way to do that. You can easily get the typed text (using any of the keyboard events like KeyDown, KeyPress etc.), but not the deleted text.

However it is not impossible to achieve this. All you need is some event/method that executes before the KeyDown event is fired. And fortunately there are many.

For this demo I will show you how to do that using the ProcessKeyMessage , but you can use any of the others as well (e.g. WndProc etc.) … Read the full article »

If x Not In (a,b,c,…) equivalent of T-SQL in VB.NET

I have seen many hits on my blog with similar search terms and also the question being asked in various forums. The answer is really very simple.
Consider the following block in SQL: … Read the full article »

Simple Text-Only WebBrowser

This is a continuation of my previous article:
Add Undo/Redo or Back/Forward functionality to your application

In the last article we constructed a class that would help us add Undo/Redo or Back/Forward functionality to our application. We read about the capabilities of the class, but a working example was missing.
In this article we will see how to create a text-only web browser for ourselves. These kinds of browsers are useful in various situations. However I won’t go into details about that at present.
The link to source code as well as executable file is given at the end of this article.
So, first we will create the basics of the web-browser, and then later on we will see how to add the Back/Forward capabilities to it. … Read the full article »

Insert a String / Text in Large File using VB.NET

Introduction

If you have been a VB programmer for a while you know that inserting strings in large files is a pain.

The most usual way is to read the whole file into a variable in memory, insert your string in that variable, and then write the whole file back to disk. This works OK for small files. But is a problem with extraordinarily large files, since it would involve large memory overheads. … Read the full article »