Click here to Skip to main content
Licence CPOL
First Posted 31 Mar 2002
Views 113,053
Bookmarked 16 times

Convert Integers to Their Textual Description

By | 19 Jan 2007 | Article
Convert an integer to its textual description

Sample Image

Preface

Ever wanted a friendlier way to display any given amount to a user? Or, perhaps you're coding a grammatically correct English application, racing game, tracking visitors, or creating an accounting solution. Whatever the case, there are some times when a programmer needs to represent a number as a written word (i.e., 4 becomes "four"). And, that's the goal of this routine.

The original version (as most typical implementations of this) used strings for calculations and conversions and while it worked, it was slower. However, I updated the algorithm to not do this anymore. As a result, I noticed an average 310% speed increase (on a Pentium IV) despite the fact I also added support for much larger numbers as well.

Usage

Using this routine is straightforward. It does make use of C-style strings as they are fast and extremely portable with most environments. The sole routine that does the grunt work is named GetNumWord() and is prototyped as follows...

// prototype/signature
char *GetNumWord (long long llNumber, char *szDest, 
                  unsigned int unLen, bool bOrdinal, bool bUseAnd);

Parameter Descriptions:

llNumber =  This is the 64-bit number to convert.
szDest =  Pointer to the output buffer (char array).
unLen =  Size in bytes of the output buffer (not string length).
bOrdinal =  Setting this to true will return the ordinal version of the number (e.g., first); otherwise, the cardinal version is returned (e.g., one).
bUseAnd =  This will determine if the and conjunction is used in the output or not (e.g., One Hundred And One).

Example

char szBuffer[100] = {0};
GetNumWord(5001, szBuffer, sizeof(szBuffer), false, false);
GetNumWord(-10, szBuffer, sizeof(szBuffer), false, false);
GetNumWord(123, szBuffer, sizeof(szBuffer), true, true);

Will return...

"Five Thousand One"
"Negative Ten"
"One Hundred And Twenty-Third"

Limitations

The function takes a long long as the number parameter. On most 32-bit and 64-bit systems, this means the lowest number you can pass is -9,223,372,036,854,775,808 and the highest number is 9,223,372,036,854,775,807.

Also, 64-bit arithmetic on a 32-bit CPU is a bit slower. However, the difference is negligible for most applications, and 64-bit CPUs are becoming more and more mainstream, so this consideration will soon be obsolete.

Credits & History

Article's Author  - Jeremy Falcon
The "and" Conjunction Suggestion  - benjymous
Memory Leak Tip  - James Curran
  • 2007-01-13 - Ver. 2.0 released.
  • 2002-04-12 - Ver. 1.1 released.
  • 2002-04-01 - Ver. 1.0 released.

License

Permission is granted to anyone to use this software for any purpose on any computer system, and to alter it and redistribute it freely, subject to the following restrictions found in NumWord.c. It's nothing big. Essentially, it states that if your computer blows up then it's not my fault, and if you use this code then give credit where it's due.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jeremy Falcon

Software Developer (Senior)
Enta USA, LLC
United States United States

Member

Jeremy finds it odd to mention himself in the third person, but alas, since nobody else can relish in the delight of Jeremy's marvelous accolades on this page, he must. And since embellishment is the way of self-centric, third-person style of writing in any good piece, it will be accepted as Jeremy's duty to master the sublime art of conceit.
 
Jeremy has conquered cancer just by writing five lines of code. Unfortunately, the government disallows that code to be revealed to the populous. Jeremy also commands time and space with his facial hair. Jeremy thrice resurrected the dead by using undocumented APIs. Jeremy can read your mind too, simply because that's just how über Jeremy is.
 
What, you don't believe that? Ok, take two...
 
Jeremy is someone who understands the importance of doing his part in giving back to the online community at large. He has been tinkering with electronics and computers in general since circa 1986, but he has studied development since 1994. Over the years he has acquired somewhat of a diverse skill set - which ranges from graphics, to technician, to programmer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFavor for a favor... some good alternatives. PinmemberShaun Harrington2:59 11 Aug '06  
GeneralRe: Favor for a favor... some good alternatives. PinmemberJeremy Falcon8:29 13 Jan '07  
Questionhow to represent 1000.25 ???? Pinmemberrenjith_sree18:20 10 Jan '05  
AnswerRe: how to represent 1000.25 ???? PinmemberJeremy Falcon7:29 13 Jan '07  
QuestionCan we have the PHP? PinmemberChinaHorse17:19 18 Mar '04  
AnswerRe: Can we have the PHP? PinmemberJeremy Falcon9:49 19 Mar '04  
GeneralRe: Can we have the PHP? PinmemberChinaHorse0:28 21 Mar '04  
GeneralRe: Can we have the PHP? PinmemberJeremy Falcon3:25 21 Mar '04  
GeneralYes, SSH PinmemberChinaHorse22:17 21 Mar '04  
GeneralRe: Yes, SSH PinmemberJeremy Falcon8:07 23 Jun '04  
GeneralIdeas Ideas! PinmemberNguyen Binh22:19 3 Apr '03  
GeneralRe: Ideas Ideas! PinmemberJeremy Falcon8:17 23 Jun '04  
GeneralBuffer overwrite waiting to happen. PinmemberAnonymous11:10 2 Jul '02  
GeneralRe: Buffer overwrite waiting to happen. PinmemberJeremy Falcon14:11 2 Jul '02  
GeneralTwo Notes.... PinmemberJames Curran3:02 8 Apr '02  
GeneralRe: Two Notes.... PinmemberJeremy Falcon4:16 8 Apr '02  
GeneralRe: Two Notes.... PinadminChris Maunder14:35 9 Apr '02  
GeneralRe: Two Notes.... PinmemberJeremy Falcon15:24 9 Apr '02  
GeneralOvercome limitation of interger to text string PinmemberNguyen Luong Son17:35 2 Apr '02  
GeneralRe: Overcome limitation of interger to text string PinmemberJeremy Falcon4:29 3 Apr '02  
GeneralFYI PinmemberJeremy Falcon15:16 2 Apr '02  
Questionhow about a redneck version? PinPopularmemberChris Losinger7:09 2 Apr '02  
AnswerRe: how about a redneck version? PinmemberJeremy Falcon9:45 2 Apr '02  
GeneralExtra addition for English users Pinmemberbenjymous3:07 2 Apr '02  
GeneralRe: Extra addition for English users PinmemberJeremy Falcon3:15 2 Apr '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 19 Jan 2007
Article Copyright 2002 by Jeremy Falcon
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid