|
On Wed, 4 Sep 2002, Booth Martin wrote:
> I have an old old routine I use for writing the prose on a check, as in:
>
> $13.95
>
> is written "Thirteen Dollars and 95 cents"
>
> It dawns on me that today's version would be a lot simpler and clearer. Any
> one have a nifty solution they'd as soon share in the archives here?
Well I have a C program to do this. It was written in an introductory C
class, so it isn't too fancy. But the concepts might help. I know it
isn't RPG but it might be worth a look anyway. Cut and pasted below.
Copy the three source files below into numbers2words.c, functions.c, and
functions.h respectively and compile with something similar to:
gcc -Wall -c numbers2words.c
gcc -Wall -c functions.c
gcc -o numbers2words numbers2words.o functions.o
(of course on iSeries the compile commands are different)
James Rich
<begin numbers2words.c>
/*This program prints out the English equivalent of a
positive integer between 0 and 999999. It reads from the
keyboard and writes to the terminal. For example, if you enter
287581, it prints out:
two hundred eighty-seven thousand five hundred eighty-one
Created: James Rich October 27, 1994
*/
#include "functions.h"
#include <stdio.h>
void
main ()
{
int num, orignum, teennum;
int quotient, teenquotient;
int remainder, teenremainder = 1;
printf ("Enter a number between 0 and 999999: ");
scanf ("%d", &num);
orignum = num;
quotient = num / 100000; /*This checks the 100000 place */
if (quotient > 0)
hundreds (quotient);
remainder = num % 100000;
num = remainder;
quotient = num / 10000; /*This checks the 10000 place */
if (quotient > 0)
{
if (quotient == 1)
{ /*Checks if number is a teen */
teennum = num;
teenremainder = teennum % 10000;
teenquotient = teenremainder / 1000;
teens (teenquotient);
teennum = 0;
}
else
tens (quotient);
}
remainder = num % 10000;
num = remainder;
quotient = num / 1000; /*This checks the 1000 place */
if (quotient > 0 && teennum != 0)
ones (quotient);
if (orignum >= 1000)
printf (" thousand ");
remainder = num % 1000;
num = remainder;
quotient = num / 100; /*This checks the 100 place */
if (quotient > 0)
hundreds (quotient);
remainder = num % 100;
num = remainder;
quotient = num / 10; /*This checks the 10 place */
if (quotient > 0)
{
if (quotient == 1)
{ /*Checks if number is a teen */
teennum = num;
teenremainder = teennum % 10;
teenquotient = teenremainder / 1;
teens (teenquotient);
teennum = 0;
}
else
tens (quotient);
}
remainder = num % 10;
num = remainder;
quotient = num / 1; /*This checks the 1 place */
if (quotient > 0 && teennum != 0)
{
ones (quotient);
}
if (orignum == 0) /*This checks if number is 0 */
printf ("zero");
printf ("\n");
}
<end numbers2words.c>
<begin functions.c>
/*This file contains the funtions for "numbers2words.c"
Created: James Rich October 27, 1994
*/
#include "functions.h"
#include <stdio.h>
void
hundreds (int quotient)
{ /*This prints the hundreds */
ones (quotient);
printf (" hundred ");
}
void
tens (int quotient)
{ /*This prints the tens */
switch (quotient)
{
case 2:
printf ("twenty-");
break;
case 3:
printf ("thirty-");
break;
case 4:
printf ("forty-");
break;
case 5:
printf ("fifty-");
break;
case 6:
printf ("sixty-");
break;
case 7:
printf ("seventy-");
break;
case 8:
printf ("eighty-");
break;
case 9:
printf ("ninety-");
break;
}
}
void
teens (int qoutient)
{ /*This prints the teens */
switch (qoutient)
{
case 0:
printf ("ten");
break;
case 1:
printf ("eleven");
break;
case 2:
printf ("twelve");
break;
case 3:
printf ("thirteen");
break;
case 4:
printf ("fourteen");
break;
case 5:
printf ("fifteen");
break;
case 6:
printf ("sixteen");
break;
case 7:
printf ("seventeen");
break;
case 8:
printf ("eighteen");
break;
case 9:
printf ("nineteen");
break;
}
}
void
ones (int quotient)
{ /*This prints the ones */
switch (quotient)
{
case 1:
printf ("one");
break;
case 2:
printf ("two");
break;
case 3:
printf ("three");
break;
case 4:
printf ("four");
break;
case 5:
printf ("five");
break;
case 6:
printf ("six");
break;
case 7:
printf ("seven");
break;
case 8:
printf ("eight");
break;
case 9:
printf ("nine");
break;
}
}
<end functions.c>
<begin functions.h>
/* Header file for "functions.c"
Created: James Rich October 27, 1994
*/
void ones(int one); /*Prints 1-9*/
void tens(int ten); /*Prints 20-90*/
void teens(int teen); /*Prints 10-19*/
void hundreds(int hundred); /*Prints 100-900*/
<end functions.h>
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.