Number Words

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

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 twenty-three thousand four hundred fifty-six" 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 ninety-nine") 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 0-999 values.

A couple of other included "enhancements":

bulletEliminate 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. 
bulletThe 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  Puzzle-A-Day Mensa Calendar.  

Running/Exploring the Program 

bullet Download Version 1 source
bullet Download Version 2 source
bullet Download Version 2 executable
 

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

 

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.