Click here to Skip to main content
11,929,675 members (58,389 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked

Convert Integers to Their Textual Description

, 6 May 2014 Apache
Rate this:
Please Sign up or sign in to vote.
Convert an integer to its textual description

Sample Image


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.


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


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"


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.


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.


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Jeremy Falcon
Software Developer (Senior)
United States United States
I've been in software development since 1994. Over the years I've learned quite a lot in what it takes to complete the process of pushing out a quality product to customers in a timely fashion. As most of my colleagues could attest, there have been many challenges in our new and growing field in the past couple of decades as the industry matures rapidly. Much more so than most others historically speaking.

As such, I've learned one of the best aspects of software engineering is embracing the change that inherently comes along with it as new technologies constantly emerge to help us improve our world one application at a time as we make sense of the overwhelming amount of data now prevalent in the Information Age.

We truly live in a time unlike that ever known to mankind in recorded history, and it is my hope to do my part to help it along to face the challenges and demands of tomorrow.

You may also be interested in...

Comments and Discussions

GeneralRe: Can we have the PHP? Pin
Jeremy Falcon21-Mar-04 4:25
memberJeremy Falcon21-Mar-04 4:25 
GeneralYes, SSH Pin
ChinaHorse21-Mar-04 23:17
memberChinaHorse21-Mar-04 23:17 
GeneralRe: Yes, SSH Pin
Jeremy Falcon23-Jun-04 9:07
memberJeremy Falcon23-Jun-04 9:07 
GeneralIdeas Ideas! Pin
Nguyen Binh3-Apr-03 23:19
memberNguyen Binh3-Apr-03 23:19 
GeneralRe: Ideas Ideas! Pin
Jeremy Falcon23-Jun-04 9:17
memberJeremy Falcon23-Jun-04 9:17 
GeneralRe: Ideas Ideas! Pin
Member 761463224-Nov-13 2:18
memberMember 761463224-Nov-13 2:18 
GeneralBuffer overwrite waiting to happen. Pin
Anonymous2-Jul-02 12:10
memberAnonymous2-Jul-02 12:10 
GeneralRe: Buffer overwrite waiting to happen. Pin
Jeremy Falcon2-Jul-02 15:11
memberJeremy Falcon2-Jul-02 15:11 
Anonymous wrote:
That just caused a buffer overwrite, because you wrote "twelve\0" into my 3-byte buffer. That's no good. Not at all.

The intent was that the caller would have enough sense to allocate a proper buffer. How many times do you see in MS documentation that the behavior is "undefined" unless you do something the way it's expected?

Regardless, you do serve a valid point that I will take into consideration. But, who am I to give credit to for the tip? I always give credit where it's do. If you prefer to stay anonymous, no problem, I just won't be able to give you credit. Frown | :(


Jeremy L. Falcon
Homepage : Sonork = 100.16311

"Victims falling under chains ~ You hear them crying dying pains
The fist of terrors breaking through ~ Now there's nothing you can do"

Song: Phantom Lord - Album: Kill 'em All - Artist: Metallica

GeneralTwo Notes.... Pin
James Curran8-Apr-02 4:02
memberJames Curran8-Apr-02 4:02 
GeneralRe: Two Notes.... Pin
Jeremy Falcon8-Apr-02 5:16
memberJeremy Falcon8-Apr-02 5:16 
GeneralRe: Two Notes.... Pin
Chris Maunder9-Apr-02 15:35
adminChris Maunder9-Apr-02 15:35 
GeneralRe: Two Notes.... Pin
Jeremy Falcon9-Apr-02 16:24
memberJeremy Falcon9-Apr-02 16:24 
GeneralOvercome limitation of interger to text string Pin
Nguyen Luong Son2-Apr-02 18:35
memberNguyen Luong Son2-Apr-02 18:35 
GeneralRe: Overcome limitation of interger to text string Pin
Jeremy Falcon3-Apr-02 5:29
memberJeremy Falcon3-Apr-02 5:29 
GeneralRe: Overcome limitation of interger to text string Pin
Member 761463224-Nov-13 2:15
memberMember 761463224-Nov-13 2:15 
GeneralFYI Pin
Jeremy Falcon2-Apr-02 16:16
memberJeremy Falcon2-Apr-02 16:16 
Questionhow about a redneck version? Pin
Chris Losinger2-Apr-02 8:09
memberChris Losinger2-Apr-02 8:09 
AnswerRe: how about a redneck version? Pin
Jeremy Falcon2-Apr-02 10:45
memberJeremy Falcon2-Apr-02 10:45 
GeneralExtra addition for English users Pin
benjymous2-Apr-02 4:07
memberbenjymous2-Apr-02 4:07 
GeneralRe: Extra addition for English users Pin
Jeremy Falcon2-Apr-02 4:15
memberJeremy Falcon2-Apr-02 4:15 
GeneralRe: Extra addition for English users Pin
Tim Smith2-Apr-02 15:56
memberTim Smith2-Apr-02 15:56 
GeneralRe: Extra addition for English users Pin
Jeremy Falcon2-Apr-02 16:14
memberJeremy Falcon2-Apr-02 16:14 
GeneralRe: Extra addition for English users Pin
benjymous2-Apr-02 22:53
memberbenjymous2-Apr-02 22:53 
GeneralNot So Unique Pin
Dave Goodman1-Apr-02 21:47
memberDave Goodman1-Apr-02 21:47 
GeneralRe: Not So Unique Pin
Christian Graus1-Apr-02 21:55
memberChristian Graus1-Apr-02 21:55 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 6 May 2014
Article Copyright 2002 by Jeremy Falcon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid