Search

 
Problem Description
This program will convert user input or random
integers in the range of 0 to 999,999 to words. 123,456 to "One
hundred twentythree thousand four hundred fiftysix" for example.
Background & Techniques
This is a beginners level program which takes about 60 lines of user
written code to do the conversion.
The program takes integers from 0 to 999,999 and converts them to
words. So, for
example, 472319 outputs "Four hundred seventy two thousand three hundred
nineteen".
The program uses "div" (integer divide) and "mod" (modular
remainder) functions to separate the individual digits to be converted.
472319 div 1000 = 472, the "thousands" part of the number while
472319 mod 1000 = 319, the "hundreds" part. This first step allows us to
convert a 6 digit number into two phrases, each part between 0 and 999
("zero" and "nine hundred ninetynine") with the word "thousand" inserted
between them.
Function "ConvertHundreds" handles converting these 0 to 999 values
into words using the same "div" and "mod" functions, dividing by 100 to get
the hundreds digit. Applying Mod 100 and
dividing the result by 10 returns the tens digit, etc. The hundreds
and units digits can be converted directly to the words "one" through "nine"
using a string array with these entries. The hundreds digit is slightly more
complicated; digits 2 through 9 can be translated to the prefix words
"twenty"
through "ninety".
The numbers between 10 and 19 are exceptions because we use the suffix
"teen" for some of the values ("fourteen" for example) and have unique names
for others ("eleven:" for example). An array of string constants indexed by
values 10 to 19 handles these exceptions. An array of digits
names 1 to 10, and tens digit prefix names 20 to 90 handle the rest of the 0999
values.
A couple of other included "enhancements":
 Eliminate commas in the input number by using the StringReplace
function. Actually, we remove the ThousandSeparator
variable which will reflect the separator character used in each
locale. 
 The Upcase function is used to capitalize the result string.

Addendum October 4, 2009: Here is Version 2 of Number Words.
Actually it is an entirely different program than the original but it does
use the same number conversion method and extends it a bit by converting in
the other direction, from number words back to numbers.
The program solves, or allows you to solve and test, the answer to
one of those self referencing statements: "This
rectangle holds _________ L's, __________ N's, and __________ vowels in
all."where the blank fields are to be filled with number words
between "one" and "twenty" which make the statement true. This
is another recent puzzle from my favorite source, the
PuzzleADay Mensa Calendar.
Running/Exploring the Program
Suggestions for Further Explorations
It would be fairly simple to extend the algorithm
to handle 9 digit or larger numbers inserting "billion", "million" etc. for
each 3 digits added.
Original Date: April, 22, 2009 
Modified:
May 12, 2018 

