Thread: Convert an amount to a string

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    Convert an amount to a string

    Good evening,

    I want to convert an amount of money e.g. 245.98$ to a string like two hundred and fourty five dollars and ninety eighty cents.

    Could you please paste me a link where i can find an algorithm for this in order to be helped ?

    Thanks, in advance!

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    search previous posts....not too long ago (maybe 2-3 weeks). Dave_Sinkula I believe it was, wrote and posted a link to this exact problem.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Well a bit difficult to find it...

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    it shouldn't be hard for you to write your own anyways

    what determines the amount of hundres you have?
    what determines the amount of 10's of dollars you have?
    ... single dollares you have?
    ... cents you have?

    algorithm
    hundreds = determine hundreds
    tens = determine tens
    singles = determine singles
    cents = determine cents

    output you have "hundreds" hundred "tens""singles" dollars and "cents" cents

    theres your algorithm

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Hint: if you divide the number by, say, one hundred using integer math, it will give you the hundreds digit.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Well i did that for the hundreds digit, but i don't know how am i supposedto take the decimal part?e.g. 123.88 ?

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    how am i supposed to take 88?

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Multiply by, say, 10, and then use the above method.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Hi,

    if the user types 123.88 i multiply it with 100 in order to take 12388 and then divide the number by ten in order to take every digit.
    What if the user types 4 or 45,then my program is going to multiply them again with 100 and i'm going to have 400 , 4500

    What shall i do then?

    I do:
    Code:
     #include <stdio.h>
    #include <stdlib.h>
    int main() {
    
    float d;
    int cnt = 1; //counts the number of digits the number has
    
    scanf("%f",&d);
    
    d= d* 100; //multiply it by 100 cause the user can input a number with two decimal digits
    
    int k = (d / 10);
    
    while( k != 0 ) {
    
           cnt = (cnt + 1);
            k = (k / 10);
    
    }
    
    }
    What shall i do ?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > d= d* 100;

    Code:
    numToWords( d / 100 );
    printf(" dollars" );
    if ( d % 100 != 0 ) {
      printf( ", and " );
      numToWords( d % 100 );
      printf( " cents.\n" );
    } else {
      printf(".\n" );
    }
    1 to 19 you pretty much do as a switch/case because it's pretty irregular, but 20 upwards has a very definite pattern if you write out some examples using lots of /10 and %10 operations.

    The only really tricky bit is working out where to start (is it hundreds or thousands), but even that works itself out with a bit of extra effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Convert it to a string and check for the presence of the '.' character. If yes, do the decimal thing, if no, don't.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Is this a function like strcpy or shall i write it myself?

  13. #13
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    My main problem is that i want to divide the number e.g. 123.88 into two integers 123 and 88.
    cause the % has to be used between two integers

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    You can use modf() to do that.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  15. #15
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Could you please send me the implementation of this modf?If of course you can find it somewhere

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  2. Having a problem!
    By Zildjian in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2004, 09:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM