Converting a number into Verbal form
Author Message
Converting a number into Verbal form

Please let me know the algorithm for converting a number into verbal form?

eg. if the number is 10,172 should be printed as "tenthousand one hundred
and sevetry two".

------------------  Posted via CNET Help.com  ------------------
http://www.*-*-*.com/
--

Fri, 29 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:

> Please let me know the algorithm for converting a number into verbal form?

> eg. if the number is 10,172 should be printed as "tenthousand one hundred
> and sevetry two".

Here's my standard example program for doing that, modulo spelling
mistakes.  If you're British, then you'll need to change the
definitions in powers[], though.

#include <stdio.h>
#include <stdlib.h>

char *ordinal (int value);

int
main (int argc, char *argv[])
{
if (argc != 2)
{
printf ("usage: ordinal <number>\n");
return EXIT_FAILURE;
}

puts (ordinal (atoi (argv[1])));
return EXIT_SUCCESS;

Quote:
}

/* Works for positive and negative numbers up to 9 digits long. */
char *
ordinal (int value)
{
static const char *const powers[]
= {"thousand", "million", "billion"};

static const char *const ones[]
= {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thir{*filter*}", "four{*filter*}", "fif{*filter*}",
"six{*filter*}", "seven{*filter*}", "eigh{*filter*}", "nine{*filter*}"};

static const char *const tens[]
= {"twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};

static char buf[1024];
char *cp = buf;

if (value < 0)
{
cp += sprintf (cp, "negative ");
value = -value;
}
else if (value == 0)
{
strcpy (buf, "zero");
return buf;
}

{
int part_stack[4];
int *part_ptr = part_stack;

for (; value; value /= 1000)
*part_ptr++ = value % 1000;

while (part_ptr > part_stack)
{
int p = *--part_ptr;

if (p >= 100)
{
cp += sprintf (cp, "%s hundred ", ones[p / 100 - 1]);
p %= 100;
}

if (p >= 20)
{
if (p % 10)
cp += sprintf (cp, "%s-%s ", tens[p / 10 - 2], ones[p % 10 - 1]);
else
cp += sprintf (cp, "%s ", tens[p / 10 - 2]);
}
else if (p > 0)
cp += sprintf (cp, "%s ", ones[p - 1]);

if (p && part_ptr > part_stack)
cp += sprintf (cp, "%s ", powers[part_ptr - part_stack - 1]);
}
}

cp[-1] = 0;
return buf;

Quote:
}

--

Sat, 30 Mar 2002 03:00:00 GMT
Converting a number into Verbal form
Show your code or do your own homework.

Quote:

> Please let me know the algorithm for converting a number into verbal form?

> eg. if the number is 10,172 should be printed as "tenthousand one hundred
> and sevetry two".

> ------------------  Posted via CNET Help.com  ------------------
>                       http://www.help.com/
> --

--

Sat, 30 Mar 2002 03:00:00 GMT
Converting a number into Verbal form
You have to write a function to do it. Why don't you have a try, and
post your code, and people will see what they can do to help you fix
it.

Oh, and please read the FAQ and ettiquette document when it comes
round next time. It has some hints on how to use this list.

On Mon, 11 Oct 1999 19:50:34 GMT, Madhava Swamy

Quote:

>Please let me know the algorithm for converting a number into verbal form?

>eg. if the number is 10,172 should be printed as "tenthousand one hundred
>and sevetry two".

>------------------  Posted via CNET Help.com  ------------------
>                      http://www.help.com/

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html
--

Sat, 30 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:

> eg. if the number is 10,172 should be printed as "tenthousand one hundred
> and sevetry two".

Standard American English would be "ten thousand, one hundred
seventy-two".

The first step is to write down the explicit transformation rules.
Then, the algorithm is rather easy.

Hint: In American usage, the top sets of 3 digits can be peeled off
independently: "WWW septillion, XXX trillion, YYY thousand, ZZZ"
where 0 factors are omitted.  Then you just need to spell out the
XXX, YYY, etc. parts which all use the same rule.  The only case
left is for exact zero, which translates to "zero".
--

Sat, 30 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

# Please let me know the algorithm for converting a number into verbal form?

# eg. if the number is 10,172 should be printed as "tenthousand one hundred
# and seventy two".

You already know it, I think. You just used that algorithm when you wrote
your question. Think about the small steps you used, like "first, I counted
the number of digits; second I grouped them in triplets; third ...". That's
about the approach I would use. Maybe I'd fold a few steps together or
change them a bit. Leaving the rest as an excercise for the prospective
algorithm guru...

You get the idea? And what was your C question? :-)

Regards,

Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--

Sat, 30 Mar 2002 03:00:00 GMT
Converting a number into Verbal form
Looks like homework to me.

Here's an idea: why don't YOU have a go at it, post your attemp,  then
people here can give advice if necessary.
Or would you prefer us to do your homework for you?

Mitch

Quote:
> Please let me know the algorithm for converting a number into verbal
form?

> eg. if the number is 10,172 should be printed as "tenthousand one hundred
> and sevetry two".

> ------------------  Posted via CNET Help.com  ------------------
>                       http://www.help.com/
> --

--

Sat, 30 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:

> eg. if the number is 10,172 should be printed as "tenthousand one hundred
> and sevetry two".

The BSD games package contains a program called "number" which
does that.  Try these URLs:

ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/games/number/number.c

ftp://ftp.netbsd.org/pub/NetBSD/NetBSD-current/tar_files/src/games.ta...
ftp://metalab.unc.edu/pub/Linux/games/bsd-games-2.8.tar.gz
ftp://ftp.debian.org/debian/dists/stable/main/source/games/bsdgames_2...
(These files contain many other games too, and are much larger.)
--

Sat, 30 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:
Ben Pfaff wrote...
>Here's my standard example program for doing that, modulo spelling
>mistakes.  If you're British, then you'll need to change the
>definitions in powers[], though.

Not if you want to make yourself understood to other British people under
the age of 90.

What you will have to do, though, is to add the word "and" after the
hundreds, e.g. "one hundred and forty-two" rather than "one hundred
forty-two".  Similarly "one thousand and forty-two", "one hundred and
forty-two thousand, one hundred and forty-two".  But of course 100 remains
just "one hundred", which will make the logic a little more complex, I
think.

Cheers,
Richard
--

Sun, 31 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:
> Hint: In American usage, the top sets of 3 digits can be peeled off
> independently: "WWW septillion, XXX trillion, YYY thousand, ZZZ"
> where 0 factors are omitted.  Then you just need to spell out the
> XXX, YYY, etc. parts which all use the same rule.  The only case
> left is for exact zero, which translates to "zero".

Four digit numbers are occasionally handled differently.
For example, 1234 can be expressed as either:
"one thousand two hundred thirty-four"
or more often "twelve hundred thirty-four"
or even simply "twelve thirty-four"

--

Sun, 31 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:

>Ben Pfaff wrote...
>>Here's my standard example program for doing that, modulo spelling
>>mistakes.  If you're British, then you'll need to change the
>>definitions in powers[], though.

>Not if you want to make yourself understood to other British people under
>the age of 90.

>What you will have to do, though, is to add the word "and" after the
>hundreds, e.g. "one hundred and forty-two" rather than "one hundred
>forty-two".  Similarly "one thousand and forty-two", "one hundred and
>forty-two thousand, one hundred and forty-two".  But of course 100 remains
>just "one hundred", which will make the logic a little more complex, I
>think.

Nope.  "and" is for the split between the integer and fractional
parts as in "seven and five eighths".

And it's another English language thread!
^^^^
Another English language thread gets its start!
^^^

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
--

Sun, 31 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:

>Looks like homework to me.

>Here's an idea: why don't YOU have a go at it, post your attemp,  then
>people here can give advice if necessary.
>Or would you prefer us to do your homework for you?

Silly question.  The latter.

Well, maybe he'd be interested in a tutor.  I've just started
tutoring someone in Java.  An interesting way to learn a language.

AFAICS, Java (like C) is a horrible language for a first
language, but I think Java may be worse.  Here is "Hello, world!":

class hello
{
public static void main (String [] args)
{
System.out.println("Hello, world!");
}
}

It makes the C version look positively friendly.

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
--

Sun, 31 Mar 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:
> Ben Pfaff wrote...

> >Here's my standard example program for doing that, modulo spelling
> >mistakes.  If you're British, then you'll need to change the
> >definitions in powers[], though.

> Not if you want to make yourself understood to other British people
> under the age of 90.

Not sure where you get the '90' from. The change to American usage
only happened a few years ago, and even then it was described as
being solely for financial matters - though that was bound to mean
that it would become universal since that's almost the only time
that such big numbers are used. I tend to use "thousand million"
which is unambiguous even if it sounds odd to some people. I don't
think I've ever needed to use bigger numbers in words.

Quote:
> What you will have to do, though, is to add the word "and" after the
> hundreds, ...

Indeed.

Sent via Deja.com http://www.deja.com/
--

Sun, 31 Mar 2002 03:00:00 GMT
Converting a number into Verbal form
Quote:
J. J. Farrell wrote...

[about use of "billion" for 10^12]

Quote:
>> Not if you want to make yourself understood to other British people
>> under the age of 90.

>Not sure where you get the '90' from.

It was a wild exaggeration, of course!

However I'm prepared to defend (though in some other group) the claim that
the older usage is already hopelessly outmoded and will not be generally
understood.  As you astutely observe, the only context in which one
frequently hears of billions is amounts of money; even then you don't
normally hear it outside the media, which universally take "billion" to mean
10^9 -- and have done for, well, as long as I remember.

(The media also use it in non-financial contexts, e.g. recent reports that
world population had reached "six billion".  IMO an announcement of "six
milliard" would have been met with a nationwide "Huh?")

Quote:
>I tend to use "thousand million"
>which is unambiguous even if it sounds odd to some people. I don't
>think I've ever needed to use bigger numbers in words.

Indeed, when I remember to do it I also try to use "thousand million", if
only to avoid conversations like this one.  :-)

Um.  I now need to hunt around for a vaguely topical observation to prevent
the moderator rejecting the article.

How about: "The only context in which *I* ever refer to thousands of million
is when talking about the ranges of integer types"?

Cheers,
Richard
--

Tue, 02 Apr 2002 03:00:00 GMT
Converting a number into Verbal form

Quote:

> Ben Pfaff wrote...
> >Here's my standard example program for doing that, modulo spelling
> >mistakes.  If you're British, then you'll need to change the
> >definitions in powers[], though.

> Not if you want to make yourself understood to other British people under
> the age of 90.

What was wrong with the milliard?
It's still used in several non-English tongues. (even non-indoeuropean
ones such as Finnish)

--
Phil Carmody
Not speaking for or on behalf of Nokia Telecommunications
--

Tue, 02 Apr 2002 03:00:00 GMT

 Page 1 of 2 [ 16 post ] Go to page: [1] [2]

Relevant Pages