2. create a lookup table (Table 1). In the table, the left column represents all the
possible digits in base ten, and the right column represents all the possible words for
the base ten digits.
If the given number has only one digit all that is needed is just a simple lookup. For
example, (using function-like notation)
Units(2) produces Two
Units(8) produces Eight
Units
Number Word
0 Zero
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
7 Seven
8 Eight
9 Nine
Table 1 – Lookup Table for One-Digit
numbers in base ten.
Converting a two digit number to words
As mentioned earlier, the one-digit case was very simple. However, the two-digit
case is not as straightforward. There are some special subcases to be considered if
conversion is to be done correctly. Table 2 shows selected two-digit words from
Ten to Ninety-Nine.
Notice from Table 2 that the following ranges 21 – 29, 31 – 39, … , 91 – 99,
all share the same word patterns. For these ranges, the word for the tens digit can
be concatenated with the word for the units digit to give the word for the two digit
number. Thus, we can see from Table 2 that
29 => 2 Tens and 9 Units => Twenty Nine
32 => 3 Tens and 2 Units => Thirty Two
91=> 9 Tens and 1 Unit => Ninety One
And so on.
3. When the unit digit is zero, then the tens digit is considered and the units digit is
ignored. For example
20 => 2 Tens and 0 Units => Twenty
30 => 3 Tens and 0 Units => Thirty
90 => 9 Tens and 0 Units => Ninety
Number Words
10 Ten
11 Eleven
12 Twelve
13 Thirteen
14 Fourteen
… …
19 Nineteen
20 Twenty
21 Twenty – One
22 Twenty – Two
… …
29 Twenty – Nine
30 Thirty
31 Thirty – One
32 Thirty – Two
…
39 Thirty – Nine
…
90 Ninety
91 Ninety – One
92 Ninety – Two
…
99 Ninety – Nine
Table 2 – Words for selected two-digit numbers
However, the range 11 – 19 breaks the rule established by the range 20–99. So,
instead of words like “Ten – One”, “Ten – Two”, “Ten – Three”… “Ten – Nine”,
we have the special names “Eleven”, “Twelve”, “Thirteen” … “Nineteen”. So, how
can we cope with these observed differences in pattern between the range 20 – 99
and the range 10 – 19?
Two more lookup tables (Table 3 and Table 4) are defined to help solve this
problem. Table 3 contains the words for the range 10 – 90, while Table 4 contains
the words for the range 11 – 19.
How can the values of the Units, Tens and Teens lookup tables be combined to get
the names of all numbers from 10 to 99? The rules defined below help to do the
work.
4. Tens
Number
(Tens Digit)
Words
1 Ten
2 Twenty
3 Thirty
4 Forty
5 Fifty
6 Sixty
7 Seventy
8 Eighty
9 Ninety
Teens
Number Words
11 Eleven
12 Twelve
13 Thirteen
14 Fourteen
15 Fifteen
16 Sixteen
17 Seventeen
18 Eighteen
19 Nineteen
Table 3 – Lookup Table for Multiples
of Ten.
Table 4 – Lookup Table for the
“Teen” numbers
Case Convert(x) will return On the condition
x has 1 digit Units(x) x is in the range 0 to 9
x has 2 digits
Tens(x(0)) x(0) is not 0 and x(1) is 0
Teens(x) x(0) is 1 and x(1) is not 0
Tens(x(0)) – Units(x(1)) x(0) is in the range 2 to 9 and
x(1) is not 0
x represents the number to be converted.
x(0) represents the first digit of x in the case where x has two digits while x(1) represents the second
digit of x.
Rules for converting a non-negative whole number x to words.
The rules here are for the case were x is at most two-digits.
Some examples will show how the rules work
Convert the following numbers to words
(a) 13 (b) 20 (c) 54 (d) 7
(a) Convert(13) = Teens(13) (since x(0) = 1 and x(1) ≠ 0)
= Thirteen (from the Teens table)
5. (b) Convert(20) = Tens(2) (since x(0) ≠ 0 and x(1) = 0)
= Twenty (from the Tens table)
(c) Convert(54) = Tens(5)–Units(4) (both x(0) and x(1) are not 0)
= Fifty–Four (from the Tens and Units Table)
= Fifty–Four
(d) Convert(7) = Units(7) (since x has one digit only)
= Seven (from the Units table)
Converting a three digit number to words
In the three-digits case, there are two sub cases to consider. The first case is when
the last two digits are zeros, while the second case is when the number formed by
the last two digits is not zero. Tables 5 and 6 show examples of each sub case.
Number Words
100 One Hundred
200 Two Hundred
300 Three Hundred
400 Four Hundred
500 Five Hundred
… …
700 Seven Hundred
800 Eight Hundred
900 Nine Hundred
Number Words
101 One Hundred and One
109 One Hundred and Nine
120 One Hundred and Twenty
127 One Hundred and Twenty Seven
504 Five Hundred and Four
540 Five Hundred and Forty
549 Five Hundred and Forty Nine
909 Nine Hundred and Nine
999 Nine Hundred and Ninety Nine
Table 5 – Case : Last two digits form “00” Table 6 – Case : Last two digits do not form “00”
Notice that the examples shown in Table 5 are pretty straightforward. That is, if the
digit string has the form x00, then the number in words will be “x Hundred”. x will
simply be taken from the Units table. So, for instance, if the digit string is “700”,
the number in words will be “Seven Hundred” since x is ”7”
When the last two digits are not “00” (the case shown in Table 6), the number in
words will have the structure “x Hundred and …”, where x is the first digit of the
three-digit number. The remaining part of the word will be formed by the conversion
of the last two digits.
The rules for the three digit case is shown below
6. Case Convert(x) will return On the condition
x has 1 digit Units(x) x is in the range 0 to 9
x has 2 digits
Tens(x(0)) x(0) is not 0 and x(1) is 0
Teens(x) x(0) is 1 and x(1) is not 0
Tens(x(0)) – Units(x(1)) x(0) is in the range 2 to 9 and
x(1) is not 0
x has 3 digits
Units(x(0)) Hundred x(0) is not 0 and
x(1) x(2) is 00
Units(x(0)) Hundred and
Convert(x(1) x(2))
x(0) is not 0 and
x(1) x(2) is not 00
x represents the number to be converted.
x(0) represents the first digit of x, x(1) represents the second digit of x, and x(2) represents the third digit of x.
x(1) x(2) represents the concatenation of the second and third digits of x. alternatively, we could say
x(1) + x(2), where “+” represents string concatenation
Rules for converting a non-negative whole number x to words.
The rules here are for the case were x is at most three-digits.
Some examples will show how the rules works
Convert the following three-digit numbers to words
(a) 500 (b) 260 (c) 801 (d) 437
(a) Convert(500) = Units(5) Hundred
= Five Hundred
(b) Convert(260) = Units(2) Hundred and Convert(60)
= Two Hundred and Tens(60)
= Two Hundred and Sixty
(c) Convert(801) = Units(8) Hundred and Convert(01)*
= Eight Hundred and Units(1)*
= Eight Hundred and One
(d) Convert(437) = Units(4) Hundred and Convert(37)
= Four Hundred and Tens(3)–Units(7)
= Four Hundred and Thirty-Seven
7. * Convert(01) = Units(1). Notice that we have two digits in the convert function. However, no condition matches
a situation when the first digit is zero. When the first digit is zero, it corresponds to the one-digit case. That is why
the Units lookup table is referenced.
Converting more than three digits
When the string of digits forms a number having more than three digits, some new
rules need to be constructed to manage the situation. However, to understand the
new set of rules which will be added to the previous set, consider what happens to
numbers when they are grouped in threes.
Since we are using the place value system, each position represents a power of Ten,
beginning with the units position (100
), the Tens position (101
), the Hundreds
position (102
), and so on. It is possible to break the digits in a number into groups
of three starting from the rightmost digit (the units digit), towards the leftmost digit.
So,
If the number has 4 digits, we would have a group of three digits and a lone
digit.
If the number has 8 digits, we would have two groups of three digits and a group
of two digits.
If we had a 12-digit number, breaking the number would result in three groups
of three digits.
and so on.
The following examples show how to represent 1234, 12345, 1234567 and
123456789 in this “expanded thousands” notation. In other words, I want to
represent the numbers in terms of powers of 1000.
1,234 = 1000 + 234 = 1 × 10001
+ 234 × 10000
12,345 = 12000 + 345 = 12 × 10001
+ 345 × 10000
1,234,567 = 1 × 10002
+ 234 × 10001
+ 567 × 10000
123,456,789 = 123 × 10002
+ 456 × 10001
+ 789 × 10000
Observe that each group of numbers can be assigned a power of 1000. Table 7
shows the powers of 1000 up to One Trillion. Since a group of digits cannot have
more than three digits, each group now has the range 0 to 999. The rules developed
for the cases One Digit, Two Digits and Three Digits can be used for each group of
three in combination with the name for the power of 1000 associated with the
group.
8. Power of
Thousand
Words
10001
Thousand
10002
Million
10003
Billion
10004
Trillion
10005
Quadrillion
10006
Quintillion
Table 7 – Names for the powers of 1000 up to the 6th
power.
For example
123,456,789 = 123 × 10002
+ 456 × 10001
+ 789 × 10000
= Convert(123) Million, Convert(456) Thousand, Convert(789)
= One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six Thousand,
Seven Hundred and Eighty-Nine
However, since we are discussing British English, a special situation arises with the
rightmost group of three digits. Within this group, if the hundreds digit is zero, the
word produced for this group will not be preceded by a comma as in the previous
example, but will be preceded by the word “and”. Some examples will illuminate this
situation.
Say, we want to convert 123,456,089
123,456,089 = 123 × 10002
+ 456 × 10001
+ 089 × 10000
= Convert(123) Million, Convert(456) Thousand and Convert(089)
= One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six Thousand and Eighty-Nine
Let us also convert 123,456,009 to words
123,456,009 = 123 × 10002
+ 456 × 10001
+ 009 × 10000
= Convert(123) Million, Convert(456) Thousand and Convert(009)
= One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six Thousand and Nine
However, how should the following numbers be handled: 123000789, 123456000
and 123000000?
9. 123,000,789 = 123 × 10002
+ 000 × 10001
+ 789 × 10000
= Convert(123) Million, Convert(789)
= One Hundred and Twenty-Three Million,
Seven Hundred and Eighty-Nine
123,456,000 = 123 × 10002
+ 456 × 10001
+ 000 × 10000
= Convert(123) Million, Convert(456) Thousand
= One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six Thousand
123,000,000 = 123 × 10002
+ 000 × 10001
+ 000 × 10000
= Convert(123) Million
= One Hundred and Twenty-Three Million
The examples only show what we want to achieve. What rules govern these
observations? The rules below show the final set of rules that can be used to convert
a number with any number of digits to words.
From the set of rules, we notice that in the case where x has more than 3 digits, a
call is made to ThousandPower. This is a lookup table for the powers of 1000
shown in Table 7. The actual ThousandPower table is shown in Table 8.
Index Word
1 Thousand
2 Million
3 Billion
4 Trillion
5 Quadrillion
6 Quintillion
Table 8 – The ThousandPower lookup table.
10. Case Convert(x) will return On the condition
x has 1 digit Units(x) x is in the range 0 to 9
x has 2 digits
Tens(x(0)) x(0) is not 0 and x(1) is 0
Teens(x) x(0) is 1 and x(1) is not 0
Tens(x(0)) – Units(x(1)) x(0) is in the range 2 to 9 and
x(1) is not 0
x has 3 digits
Units(x(0)) Hundred x(0) is not 0 and
x(1) x(2) is 00
Units(x(0)) Hundred and
Convert(x(1) x(2))
x(0) is not 0 and
x(1) x(2) is not 00
x has more
than 3 digits
Convert(Prefix(x , r)) ThousandPower(q) RemainingDigits is 0
Convert(Prefix(x , r)) ThousandPower(q) and
Convert(RemainingDigits)
RemainingDigits is within the
range 1 to 99 inclusive
Convert(Prefix(x , r)) ThousandPower(q) ,
Convert(RemainingDigits)
If RemainingDigits is 100 or
above
x represents the number to be converted.
x(0) represents the first digit of x, x(1) represents the second digit of x, and x(2) represents the third digit of x.
x(1) x(2) represents the concatenation of the second and third digits of x. alternatively, we could say
x(1) + x(2), where “+” represents string concatenation
RemainingDigits = Parse(Suffix(x , Length(x) – Length(Prefix(x))))
RemainingDigits stores the last digits remaining after extracting the prefix substring of the digit string x
Parse trims any leading zeros from the suffix substring
Suffix(x , n) returns the last n characters of the string x
Prefix(x , r) returns the first r characters of the string x
Length(x) returns the length of the string x
r = Length(x) mod 3
The mod operator returns the remainder after division.
However, this one used here is special, since it returns 3 if the remainder is zero.
It is used to find the first r leading digits of the digit string x
q = (Length(x) – r) / 3
q represents the power of 1000 for the leading r digits of the digit string x
Some examples (for four or more digits) will illustrate how the rules work. For
brevity, not all function calls will be shown.
11. Convert the following numbers to words
(a) 1234
(b) 12345
(c) 1234567
(d) 123456009
(e) 123000789
(f) 123456000
(g) 123000000
(a) Convert(1234) = Convert(1) ThousandPower(1), Convert(234)
= One ThousandPower(1), Convert(234)
= One Thousand, Convert(234)
= One Thousand, Units(2) Hundred and Convert(34)
= One Thousand, Two Hundred and Tens(3)-Units(4)
= One Thousand, Two Hundred and Thirty-Four
(b) Convert(12345) = Convert(12) ThousandPower(1), Convert(345)
= Twelve ThousandPower(1), Convert(345)
= Twelve Thousand, Convert(345)
= Twelve Thousand, Units(3) Hundred and Convert(45)
= Twelve Thousand, Three Hundred and Tens(4)-Units(5)
= Twelve Thousand, Three Hundred and Forty-Five
(c) Convert(1234567)
= Convert(1) ThousandPower(2),
Convert(234567)
= One ThousandPower(2),
Convert(234567)
= One Million,
Convert(234567)
= One Million,
Convert(234) ThousandPower(1),
Convert(567)
= One Million,
Units(2) Hundred and Convert(34) ThousandPower(1),
Convert(567)
= One Million,
Two Hundred and Convert(34) ThousandPower(1),
Convert(567)
12. = One Million,
Two Hundred and Tens(3)-Units(4) ThousandPower(1),
Convert(567)
= One Million,
Two Hundred and Thirty-Four ThousandPower(1),
Convert(567)
= One Million,
Two Hundred and Thirty-Four Thousand,
Convert(567)
= One Million,
Two Hundred and Thirty-Four Thousand,
Units(5) Hundred and Convert(67)
= One Million,
Two Hundred and Thirty-Four Thousand,
Five Hundred and Convert(67)
= One Million,
Two Hundred and Thirty-Four Thousand,
Five Hundred and Tens(6)-Units(7)
= One Million,
Two Hundred and Thirty-Four Thousand,
Five Hundred and Sixty-Seven
(d) Convert(123456009)
= Convert(123) ThousandPower(2),
Convert(456009)
= One Hundred and Twenty-Three ThousandPower(2),
Convert(456009)
= One Hundred and Twenty-Three Million,
Convert(456009)
= One Hundred and Twenty-Three Million,
Convert(456) ThousandPower(1) and
Convert(009)
= One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six ThousandPower(1) and
Convert(009)
13. = One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six Thousand and
Convert(009)
= One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six Thousand and
Nine
(e) Convert(123000789)
= Convert(123) ThousandPower(2),
Convert(000789)
= One Hundred and Twenty-Three ThousandPower(2),
Convert(000789)
= One Hundred and Twenty-Three Million,
Convert(000789)
= One Hundred and Twenty-Three Million,
Convert(789)
= One Hundred and Twenty-Three Million,
Seven Hundred and Eighty-Nine
(f) Convert(123456000)
= Convert(123) ThousandPower(2),
Convert(456000)
= One Hundred and Twenty-Three ThousandPower(2),
Convert(456000)
= One Hundred and Twenty-Three Million,
Convert(456000)
= One Hundred and Twenty-Three Million,
Convert(456) ThousandPower(1)
= One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six ThousandPower(1)
= One Hundred and Twenty-Three Million,
Four Hundred and Fifty-Six Thousand
(g) Convert(123000000)
= Convert(123) ThousandPower(2)
= One Hundred and Twenty-Three ThousandPower(2)
= One Hundred and Twenty-Three Million
14. Implementing the conversion function for changing figures
to words
The Demo application is implemented using Visual Basic.NET 2010 as a windows
forms application. Some screenshots of the running application are shown below.
Notice from the screenshots that the form makes use of a textbox to accept the
input as a string of digits (InputTextBox), a button (Button1), and a multiline
textbox for the output (OutputTextBox).
15. Using a WinForms implementation allows me define the lookup tables as attributes
of the form (Form1). Then, the lookup tables can be initialized with the necessary
values within the Form Load event handler. Once the tables are loaded, they do not
need to be loaded again for the entire existence of the form. The attribute definition
for the Form1 class is shown in the source code listing below.
Public Class Form1
' This structure stores the names of single digit numbers
Private _Units As SortedList(Of String, String)
' This sorted list stores the words for two digit numbers
' ending with zero (from 10 to 90)
Private _Tens As SortedList(Of String, String)
' This sorted list stores the words for 11 to 19
Private _Teens As SortedList(Of String, String)
' This sorted list stores the words for the powers
' of 1000, up to power 6
Private _ThousandPowers As SortedList(Of Integer, String)
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Me._Units = New SortedList(Of String, String)
Me._Tens = New SortedList(Of String, String)
Me._Teens = New SortedList(Of String, String)
Me._ThousandPowers = New SortedList(Of Integer, String)
16. With Me._Units
.Add("0", "Zero")
.Add("1", "One")
.Add("2", "Two")
.Add("3", "Three")
.Add("4", "Four")
.Add("5", "Five")
.Add("6", "Six")
.Add("7", "Seven")
.Add("8", "Eight")
.Add("9", "Nine")
End With
With Me._Tens
.Add("1", "Ten")
.Add("2", "Twenty")
.Add("3", "Thirty")
.Add("4", "Forty")
.Add("5", "Fifty")
.Add("6", "Sixty")
.Add("7", "Seventy")
.Add("8", "Eighty")
.Add("9", "Ninety")
End With
With Me._Teens
.Add("11", "Eleven")
.Add("12", "Twelve")
.Add("13", "Thirteen")
.Add("14", "Fourteen")
.Add("15", "Fifteen")
.Add("16", "Sixteen")
.Add("17", "Seventeen")
.Add("18", "Eighteen")
.Add("19", "Nineteen")
End With
With Me._ThousandPowers
.Add(1, "Thousand")
.Add(2, "Million")
.Add(3, "Billion")
.Add(4, "Trillion")
.Add(5, "Quadrillion")
.Add(6, "Quintillion")
End With
End Sub
End Class
17. Each time the button is clicked, the event handler (in this case the Button1 click
event handler) calls the Convert function to convert the Text contents of the
InputTextBox to words. If something other than a string of digit is entered by the
user, the function Convert will throw an exception.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Try
' Call the Converter to return the number in words
' It could throw an exception if the input is not valid
OutputTextbox.Text = Convert(InputTextBox.Text)
Catch ex As Exception
OutputTextbox.Text = ex.Message
End Try
End Sub
The Convert function makes use of the ULong data type to represent the string
converted from the textbox. ULong can represent integers from 0 to
18,446,744,073,709,551,615 (that is, over 18 quintillion). Any number bigger than
this causes the function Parse to throw an “Out of range” exception.
Private Function Convert(ByVal s As String) As String
' Converts the input number 's' to words
Try
' First parse the input string to confirm
' whether the input is a valid number
' Also, any leading zeros will be removed
Dim digitString As String = ULong.Parse(s).ToString
' Check for the number of digits in digitString
Select Case digitString.Length
Case 1 ' digitString is a one digit number
' Return the word for the one-digit number
Return Me._Units(digitString)
Case 2 ' digitString is a two digit number
Dim firstDigit As String = digitString(0)
Dim lastDigit As String = digitString(1)
18. If lastDigit = "0" Then
' This case is for 10, 20, 30, 40, ..., 80, 90
Return Me._Tens(firstDigit)
ElseIf firstDigit = "1" And "123456789".Contains(lastDigit) Then
' This case is for 11, 12, 13, 14, ..., 18, 19
Return Me._Teens(digitString)
ElseIf "23456789".Contains(firstDigit) And "123456789".Contains(lastDigit) Then
' This case is for 21 to 29, 31 to 39, ..., 81 to 89, 91 to 99
Return Me._Tens(firstDigit) + "-" + Me._Units(lastDigit)
End If
Case 3 ' digitString is a three digit number
Dim firstDigit As String = digitString(0)
Dim lastTwoDigits As String = digitString(1) + digitString(2)
If lastTwoDigits = "00" Then
' This case is for 100, 200, 300, 400, ..., 800, 900
Return Me._Units(firstDigit) + " Hundred"
Else
' This case is for 101 to 199, 201 to 299, ..., 801 to 899, 901 to 999
Return Me._Units(firstDigit) + " Hundred and " + Convert(lastTwoDigits)
End If
Case Is > 3 ' digitString has more than three digits
' The prefix is the leftmost group of digits.
' It could be One, Two or Three digits
Dim prefix As String = Strings.Left(digitString, Modn(digitString.Length, 3))
' thPower stores the power of 1000 for the prefix
Dim thPower As Integer = (digitString.Length - prefix.Length) 3
' Once the prefix is gotten, the leftover substring of digits is the suffix.
' Since the suffix might have leading zeroes, it has to be parsed (to remove any
' leading zeroes) and stored in the variable remainingDigits
Dim remainingDigits As String = ULong.Parse(Strings.Right(digitString, _
digitString.Length - prefix.Length)).ToString
If remainingDigits = "0" Then
' This happens when remainingDigits reduces to 0
Return Convert(prefix) + " " + Me._ThousandPowers(thPower)
ElseIf remainingDigits.Length = 1 Or remainingDigits.Length = 2 Then
' This happens when remainingDigits reduces to a one-digit number not equal to
' 0 or reduces to a two-digit number
Return Convert(prefix) + " " + Me._ThousandPowers(thPower) + " and " _
+ Convert(remainingDigits)
Else
Return Convert(prefix) + " " + Me._ThousandPowers(thPower) + ", " _
+ Convert(remainingDigits)
End If
19. Case Else
Return ""
End Select
Catch ex As Exception
Throw New Exception("Out Of Range or Invalid Number")
End Try
End Function
In Visual Basic, the Mod operator returns the remainder after division. Thus, 6 Mod 3
will return 0. However, in order to extract the prefix substring – the leftmost group
of 1, 2 or 3 digits – a new kind of Mod operator is needed that will return the divisor
if the remainder is zero. Thus,
6 Mod 3 = 0 for the normal Mod operator
6 Modn 3 = 3 for the special Mod operator
The special mod operator is defined below
Private Function Modn(ByVal a As UInteger, ByVal b As UInteger) As UInteger
' Returns the remainder after dividing a by b
' Special Cases:
' If the remainder is 0 and a is 0, it returns 0
' If the remainder is 0 and a is not 0, it returns b
' Mod will throw a "Division by Zero" exception if b = 0
Try
Dim r As Integer = a Mod b
If r = 0 Then
If a = 0 Then
Return 0
Else
Return b
End If
Else
Return r
End If
Catch ex As Exception
Throw ex
End Try
End Function
20. From DanieldotNET…
I would like to thank my readers for your interest in my documents.
Your suggestions and comments are also welcome.
If you like this article, please freely share it with others