Quantcast
Channel: Pradeep1210's Blog
Viewing all articles
Browse latest Browse all 18

Number to Words Converter

0
0

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



Viewing all articles
Browse latest Browse all 18

Latest Images

Trending Articles





Latest Images