Vous êtes sur la page 1sur 20

How To:

Convert a number from figures to words


DanieldotNET FEB2014 This article analyzes the problem of converting a non-negative, two-digit whole number from figures to words. Only British English words are considered. It also provides an implementation of the solution in Visual Basic.NET 2010. Contents Converting a single digit number to words Converting a two digit number to words Converting a three digit number to words Converting more than three digits Implementing the conversion function for changing figures to words We are accustomed to expressing numbers in words during normal everyday speech. However, spelling out the rules for doing this could be comparatively difficult. This becomes obvious when trying to express the rules in form of a computer program. This article analyzes this problem beginning with the single digit case. It then discusses how to convert a number having two, three, and more than three digits to words. Only base ten numbers are considered in this article. The approach used in this article is that that of lookup tables. That is, several lookup tables are used to store the words for special numbers. Then, other rules are used to combine values retrieved from the tables, sometimes recursively, in order to get the word for other numbers not defined in the tables. The following sections discuss this approach in more detail.

Converting a single digit number to words


The problem of converting figures to words can be viewed as a language translation problem. Here, the translation is from a mathematical, place value notation to British English. The problem can be split into two cases: a one-digit case and a twodigit case. First, how could one express a single-digit number in words? This part is really straightforward. Since the number base is base ten, the possible one-digit numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. What can be done here is to

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

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.

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

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.

Tens
Number (Tens Digit) Words

1 2 3 4 5 6 7 8 9

Ten Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety

Teens Number 11 12 13 14 15 16 17 18 19

Words Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen

Table 3 Lookup Table for Multiples of Ten.

Table 4 Lookup Table for the Teen numbers

Case x has 1 digit

Convert(x) will return Units(x) Tens(x(0))

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

Teens(x) Tens(x(0)) Units(x(1))

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)

(a) Convert(13) = Teens(13) = Thirteen

(b) Convert(20) = Tens(2) = Twenty

(since x(0) 0 and x(1) = 0) (from the Tens table)


(both x(0) and x(1) are not 0) (from the Tens and Units Table)

(c) Convert(54) = Tens(5)Units(4) = FiftyFour = FiftyFour (d) Convert(7) = Units(7) = Seven

(since x has one digit only) (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 100 200 300 400 500 700 800 900 Words One Hundred Two Hundred Three Hundred Four Hundred Five Hundred Seven Hundred Eight Hundred Nine Hundred Number 101 109 120 127 504 540 549 909 999 Words One Hundred and One One Hundred and Nine One Hundred and Twenty One Hundred and Twenty Seven Five Hundred and Four Five Hundred and Forty Five Hundred and Forty Nine Nine Hundred and Nine 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

Case x has 1 digit

Convert(x) will return Units(x) Tens(x(0))

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

Teens(x) Tens(x(0)) Units(x(1)) Units(x(0)) Hundred

x has 3 digits

Units(x(0)) Hundred and Convert(x(1) x(2))

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.

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.

Power of Thousand 10001 10002 10003 10004 10005 10006

Words Thousand Million Billion Trillion Quadrillion 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?

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

Table 8 The ThousandPower lookup table.

Case x has 1 digit

Convert(x) will return Units(x) Tens(x(0))

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

Teens(x) Tens(x(0)) Units(x(1)) Units(x(0)) Hundred

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 has more than 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 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

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).

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

"Ten") "Twenty") "Thirty") "Forty") "Fifty") "Sixty") "Seventy") "Eighty") "Ninety")

"Eleven") "Twelve") "Thirteen") "Fourteen") "Fifteen") "Sixteen") "Seventeen") "Eighteen") "Nineteen")

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

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

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

Vous aimerez peut-être aussi