Converting Numbers to Words

This is a discussion on Converting Numbers to Words within the C Programming forums, part of the General Programming Boards category; Let's start from scratch. First, you need to be able to convert values from 0 thru 999 correctly. The hundred's ...

  1. #16
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Let's start from scratch.

    First, you need to be able to convert values from 0 thru 999 correctly. The hundred's is easy. Then the ten's, then the one's.

    Second, if ten's = 1 you have a special case. Add that logic next

    Third, add the logic that does the above for every 3 digits to get the thousand, million, billion... you need to count from the right and skip every 3 values until you get to the highest triple, the process as above following it with the magnitude (million, trillion, etc.) The back yo 3 values and do it again.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  2. #17
    Registered User denizengt's Avatar
    Join Date
    Sep 2003
    Posts
    19

    Talking

    Ok, so I managed to do it! Converting numbers to words that is. Thanks everyone

    I've run into another problem.


    I have a number entered as words and stored in a string. I want to convert it back into numbers: Here's my psuedo

    Code:
    String contains words.
    
    Search string for highest order word
    
    Search for "million" term first, if not found, "thousands", if not found "hundred"
    
    Get numbers that pre-cede "million" term if found.
    
    Convert these numbers (in word form) to numerical. Search word list for number.
    
    Check if string has ended. 
    
    If string hasn't ended. Search for "thousand "term. 
    
    If found, get numbers that precede it.
    
    Convert these numbers to numerical. Search word list for number.
    
    If string hasn't ended, Search for "hundred" term.
    
    etc..

    The problem I am having is, when I find the "million" term for instance, how do I retrieve the 1 to 3 words that occur before it, (and hence pass them to my "word lists" and retrieve the int equivalent to add) ?

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I know you already solved the problem, but hey, what's another example?

    Code:
    char * printcheck(char buffer[], char number[])
    {
     char * hundreds_n_ones[] = 
     {
      "", 
      "One ", 
      "Two ", 
      "Three ", 
      "Four ", 
      "Five ", 
      "Six ", 
      "Seven ", 
      "Eight ", 
      "Nine " 
     };
     
     char * specials[] = 
     {
      "Ten ", 
      "Eleven ", 
      "Twelve ",
      "Thirteen ", 
      "Fourteen ", 
      "Fifteen ", 
      "Sixteen ", 
      "Seventeen ", 
      "Eighteen ", 
      "Nineteen " 
     };
     
     char * tens[] = 
     {
      "",
      "", 
      "Twenty ", 
      "Thirty ",
      "Forty ", 
      "Fifty ", 
      "Sixty ", 
      "Seventy ", 
      "Eighty ", 
      "Ninety " 
     };
     
     char * levels [] = 
     {
      "",
      "Thousand ", 
      "Million ", 
      "Billion ",
      "Trillion ", 
      "Quadrillion ", 
      "Quintillion ", 
      "Sextillion ", 
      "Septillion ", 
      "Octillion ", 
      "Nintillion ",
      "Lots " 
     };
     
     buffer[0] = 0;
     
     int length = strlen(number);
     
     if(length == 0) return buffer;
      
     int digit, mode, level, place = length;
     
     bool special = false;
    
     int max_level_index = sizeof(levels)/sizeof(levels[0])-1;
     
         for(int i = 0; i < length; ++i, --place)
       {
        digit = number[i]-'0';
        
        mode = place%3;
         
           if(mode == 1)
          {
              if(special)
             {  
              special = !special;
              
              strcat(buffer, specials[digit]); 
             }
              else
               strcat(buffer, hundreds_n_ones[digit]);
               
           level = (place-1)/3;
             
              if(level)
             {
                 if(level > max_level_index)
                  level = max_level_index;
              
              digit = level;
              
              strcat(buffer, levels[digit]);                    
             }      
          }       
           else if(mode == 2)
          {
              if(digit == 1)
               special = true;
              else
               strcat(buffer, tens[digit]);
          }       
           else if(mode == 0)
          {
           strcat(buffer, hundreds_n_ones[digit]);
           
           strcat(buffer, "Hundred ");
          } 
       }
      
     return buffer;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #19
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by denizengt The problem I am having is, when I find the "million" term for instance, how do I retrieve the 1 to 3 words that occur before it, (and hence pass them to my "word lists" and retrieve the int equivalent to add) ?
    I'll try this for the third time....

    If you found MILLION, copy everything from the beginning of he string to MILLION into a temp array, and process that array.

    Then skip over MILLION, remember this position 'p' and search for THOUSAND. Repeat above from 'p' to the THOUSAND.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #20
    Registered User denizengt's Avatar
    Join Date
    Sep 2003
    Posts
    19
    Ok, sorry to resurrect this issue once more.

    I've got numbers to words working. Now, what I have to do, is convert numbers entered into stdin, into their word equivalents! I have spent a few weeks trying to figure out how to do this.


    I think the problem is with my approach. Can anybody tell me how they would go about approaching this problem. I just can't seem to get my head around going the other way

    I need to "convert" numbers entered in literal form, say STDIN = "one hundred billion" and produce the numerical equivalent. I know you've tried to help me, but I still can't do it.

  6. #21
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    you could have a single int, say int sum, and give each word its respective value. Then go about with commands like for "Twenty" do int=+20, "million" does sum=+1 000 000 and so on.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 12:38 PM
  2. Converting words into ASCII values
    By manutdfan in forum C Programming
    Replies: 3
    Last Post: 01-22-2007, 11:22 AM
  3. converting numbers into words
    By Kozam in forum C Programming
    Replies: 2
    Last Post: 09-30-2003, 07:49 AM
  4. C++ program help(converting numbers to english words)
    By bama1 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 01:17 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21