Académique Documents
Professionnel Documents
Culture Documents
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 0 1 2 3 4 5 6 7 8 9
Word Zero One Two Three Four Five Six Seven Eight Nine
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 10 11 12 13 14 19 20 21 22 29 30 31 32 39 90 91 92 99 Words Ten Eleven Twelve Thirteen Fourteen Nineteen Twenty Twenty One Twenty Two Twenty Nine Thirty Thirty One Thirty Two Thirty Nine Ninety Ninety One Ninety Two Ninety Nine
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.
Tens
Number (Tens Digit) Words
1 2 3 4 5 6 7 8 9
Teens Number 11 12 13 14 15 16 17 18 19
Words Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen
On the condition x is in the range 0 to 9 x(0) is not 0 and x(1) is 0 x(0) is 1 and x(1) is not 0 x(0) is in the range 2 to 9 and x(1) is not 0
x has 2 digits
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
(since x(0) = 1 and x(1) 0) (from the Teens table)
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
On the condition x is in the range 0 to 9 x(0) is not 0 and x(1) is 0 x(0) is 1 and x(1) is not 0 x(0) is in the range 2 to 9 and x(1) is not 0 x(0) is not 0 and x(1) x(2) is 00 x(0) is not 0 and x(1) x(2) is not 00
x has 2 digits
x has 3 digits
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
* 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.
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.
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?
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 1 2 3 4 5 6 Word Thousand Million Billion Trillion Quadrillion Quintillion
On the condition x is in the range 0 to 9 x(0) is not 0 and x(1) is 0 x(0) is 1 and x(1) is not 0 x(0) is in the range 2 to 9 and x(1) is not 0 x(0) is not 0 and x(1) x(2) is 00 x(0) is not 0 and x(1) x(2) is not 00 RemainingDigits is 0 RemainingDigits is within the range 1 to 99 inclusive If RemainingDigits is 100 or above
x has 2 digits
x has 3 digits
Units(x(0)) Hundred and Convert(x(1) x(2)) Convert(Prefix(x , r)) ThousandPower(q) Convert(Prefix(x , r)) ThousandPower(q) and Convert(RemainingDigits) Convert(Prefix(x , r)) ThousandPower(q) , Convert(RemainingDigits)
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.
Convert the following numbers to words (a) (b) (c) (d) (e) (f) (g) 1234 12345 1234567 123456009 123000789 123456000 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 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 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)
(b)
(c)
= 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)
= 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
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)
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", .Add("2", .Add("3", .Add("4", .Add("5", .Add("6", .Add("7", .Add("8", .Add("9", End With With Me._Teens .Add("11", .Add("12", .Add("13", .Add("14", .Add("15", .Add("16", .Add("17", .Add("18", .Add("19", 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
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)
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
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 6 Modn 3 = 3 for the normal Mod operator for the special Mod operator
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