Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Float to Printf structure

Status
Not open for further replies.

MrVariable

Programmer
Feb 5, 2003
1
0
0
GB
OK, I am writing a program that when you enter a number (23456.75) will then convert it to the appropriate text E.G:

Twenty Three Thousand Four Hundred and Fifty Six Pounds And Seventy Five Pence

I have got it working except with 11111.11 cos this returns:

Eleven thousand Ten One Pounds And Ten One Pence

Obviously a Major cock-up, The code i am using is massive cosisting of lots of case statements, printf, and if statements, any idesas on a much easier way that wil work?
 
I think much easyer will be if you transform your float to a string and process characters.

Ion Filipski
1c.bmp
 
Here's a little something I just scribbled.
The only poor thing about it is it's overuse of the word 'and' for very large numbers.

Eg.
Code:
Enter num > 1234567
1234567 = one million and two hundred and thirty four thousand and five hundred and sixty seven
Hopefully, it will provide some inspiration for you :)

Code:
#include <stdio.h>
#include <string.h>

void num2words ( int num, char *buff ) {
    static char *units[] = {
        &quot;&quot;, &quot;one&quot;, &quot;two&quot;,  &quot;three&quot;,  &quot;four&quot;,
        &quot;five&quot;,  &quot;six&quot;,  &quot;seven&quot;,  &quot;eight&quot;,  &quot;nine&quot;,
        &quot;ten&quot;,  &quot;eleven&quot;,  &quot;twelve&quot;,  &quot;thirteen&quot;,  &quot;fourteen&quot;,
        &quot;fifteen&quot;,  &quot;sixteen&quot;,  &quot;seventeen&quot;,  &quot;eighteen&quot;,  &quot;nineteen&quot;,
    };
    static char *tens[] = {
         &quot;&quot;,  &quot;&quot;,  &quot;twenty&quot;,  &quot;thirty&quot;,  &quot;fourty&quot;,
         &quot;fifty&quot;,  &quot;sixty&quot;,  &quot;seventy&quot;,  &quot;eighty&quot;,  &quot;ninety&quot;,
    };
    int part;

    part = num / 1000000;
    num = num % 1000000;
    if ( part != 0 ) {
        num2words( part, buff );
        strcat( buff, &quot; million&quot; );
        if ( num != 0 ) strcat( buff, &quot; and &quot; );
    }

    part = num / 1000;
    num = num % 1000;
    if ( part != 0 ) {
        num2words( part, buff );
        strcat( buff, &quot; thousand&quot; );
        if ( num != 0 ) strcat( buff, &quot; and &quot; );
    }

    part = num / 100;
    num = num % 100;
    if ( part != 0 ) {
        num2words( part, buff );
        strcat( buff, &quot; hundred&quot; );
        if ( num != 0 ) strcat( buff, &quot; and &quot; );
    }

    if ( num < 20 ) {
        strcat( buff, units[num] );
    } else {
        strcat( buff, tens[num/10] );
        strcat( buff, &quot; &quot; );
        num2words( num % 10, buff );
    }
}

int main ( ) {
    char buff[200];
    int val;
    while ( printf( &quot;Enter num > &quot; ) && scanf( &quot;%d&quot;, &val ) == 1 ) {
        *buff = '\0';
        num2words( val, buff );
        printf( &quot;%d = %s\n&quot;, val, buff );
    }
    return 0;
}

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top