Number to Word (Billions)

This is a discussion on Number to Word (Billions) within the C Programming forums, part of the General Programming Boards category; Originally Posted by Perspective Here's an idea. Code: /* returns string representation of number in ones column. Ex. 3 -> ...

  1. #31
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Question Question

    Quote Originally Posted by Perspective View Post
    Here's an idea.
    Code:
    /* returns string representation of number in ones column. Ex. 3 -> three */
    string ones(int num);
    
    /* returns string representation of number in tens column. Ex. 3 -> thirty */
    string tens(int num);
    Now break the numbers down into the hundreds form for each significant place (billions, millions, thousands, hundreds). Use the % operator to get each value and the above functions to get the string representations of those values. Then stick them all together with the terms for each significant place.

    Code:
    ex:
    31, 821
      -> 031 
      -> 0 -> ""
      -> 3 -> thirty
      -> 1 -> one
    Thousand
      -> 821
      -> 8 -> eight (using ones column name since it's the same as hundreds)
    Hundred
      -> 21 -> 2 -> twenty
            -> 1 -> one
    
    Result: Thirty one thousand eight hundred twenty one.
    I've been trying to understand this but i can't fully come up with
    a full idea. I mean, i can't understand:
    "string ones(int num);" or how to use this.
    and also, how to separate the Billions, millions, thousands and ones colums, and the decimal.
    Hope you could help more, THANKS!

  2. #32
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    here's the program that's already working.
    hope you could help on shortening it.
    please? thanks!

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    char numin[100];
    
    int pesonum = 0;
    int centnum = 0;
    int decpoint = 0;
    int xcounter = 0;
    int ycounter = 0;
    
    void onesplace(char);
    void othercase(char);
    void tensplace(char);
    void hundreds(char);
    void thousands(char);
    void tenthou(char);
    void hundredthou(char);
    void millions(char);
    void tenmill(char);
    void hundredmill(char);
    void billions(char);
    void tenbill(char);
    void hundredbill(char);
    
    
    
    
    int main (void)
    {
    	clrscr();
    	printf("enter amount: ");
    	fgets(numin,100,stdin);
    
    	do
    	{
    
    		for(xcounter = 0; xcounter<=strlen(numin); xcounter++)
    		{
    			if(numin[xcounter] == ',')
    			{
    				for(ycounter = xcounter; ycounter<strlen(numin); ycounter++)
    				{
    					numin[ycounter] = numin[ycounter+1];
    				}
    			}
    			else
    				continue;
    	
    		}
    
    	}
    
    	while(strpbrk(numin,","));
    
    
    	for(xcounter = 0; xcounter<=strlen(numin); xcounter++)
    	{
    		if(numin[0] == '0')
    		{
    			for(ycounter = 0; ycounter<strlen(numin); ycounter++)
    			{
    				numin[ycounter] = numin[ycounter+1];
    			}
    		}
    		else
    			break;
    	}
    
    
    	if(numin[0] == '.')
    	{
    		printf("zero ");
    	}
    
    	else if(strlen(numin)>18)
    	{
    		printf("Number exceeds limit!");
    		exit(0);
    	}
    
    	
    	for(xcounter=0; xcounter<strlen(numin); xcounter++)
    	{
    		if (numin[xcounter] == '.')
    		{
    			pesonum = xcounter - 1;
    			centnum = xcounter + 1;
    			decpoint = 1;
    			break;
    		}
    	
    		else
    		{
    			pesonum = strlen(numin) - 2;
    			decpoint = 0;	
    		}
    	}	
    
    
    	switch(pesonum+1)
    	{		
    		case 12:
    			hundredbill(numin[pesonum-11]);
    		case 11:
    			tenbill(numin[pesonum-10]);
    		case 10:
    			if (numin[pesonum-10] == '1')
    			{
    				othercase(numin[pesonum-9]);
    				printf("billion ");
    			}
    		
    			else
    			{
    				billions(numin[pesonum-9]);
    				printf("billion ");
    			}
    		case 9:
    			hundredmill(numin[pesonum-8]);
    		case 8:
    			tenmill(numin[pesonum-7]);
    		case 7:
    			if (numin[pesonum-7] == '1')
    			{
    				othercase(numin[pesonum-6]);
    				printf("million ");
    			}
    		
    			else if ((numin[pesonum-7] != '0')||(numin[pesonum-8] != '0')||(numin[pesonum-6] != '0'))
    			{
    				millions(numin[pesonum-6]);
    				printf("million ");
    			}
    		case 6:
    			hundredthou(numin[pesonum-5]);
    		case 5:
    			tenthou(numin[pesonum-4]);
    		case 4:	
    			if (numin[pesonum-4] == '1')
    			{
    				othercase(numin[pesonum-3]);
    				printf("thousand ");
    			}
    
    			else if ((numin[pesonum-4] != '0')||(numin[pesonum-5] != '0')||(numin[pesonum-3] != '0'))
    			{ 
    				thousands(numin[pesonum-3]);
    				printf("thousand ");
    			}
    		case 3:
    			hundreds(numin[pesonum-2]);
    		case 2:
    			tensplace(numin[pesonum-1]);
    		case 1:
    			if (numin[pesonum-1] == '1')
    			{
    				othercase(numin[pesonum]);
    			}
    
    			else 
    			{
    				onesplace(numin[pesonum]);
    			}
    	}
    
    	if(decpoint==1)
    	{
          	if((numin[centnum] =='0') && (numin[centnum+1] =='0'))
          	{
    			printf("pesos and zero centavos");
           	}
    
    	 	else if((numin[strlen(numin)-3] == '.') && (numin[strlen(numin)-2] == '0'))
    	 	{
    			printf("pesos and zero centavos");
    	 	}
    
           	else
           	{
    			printf("pesos and ");
    
    			tensplace(numin[centnum]);
    
    			if (numin[centnum] == '1')
    			{
    				othercase(numin[centnum+1]);
    			}
    
                	else
    			{
    				onesplace(numin[centnum+1]);
    			}
    
    			printf("centavos");
            	}
    	}
    
    	else if(decpoint==0)
    	{
    		printf("pesos");
    	}
    
    	getch();
    
    	return 0;
    
    }
    
    
    
    void othercase(char onesplace)
    {
    	switch(onesplace)
    	{
    		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 onesplace(char onesplace)
    {
    
    	switch(onesplace)
    	{
    		case '0':
    		{
    			break;
    		}
    		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;
    		}
    	}
    }	
    
    
    
    void tensplace(char tensplace)
    {
    	switch(tensplace)
    	{
    		case '1':
    		{
    			break;
    		}
    		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 hundreds(char hundreds)
    {
    	if (hundreds != '0')
    	{
    		onesplace(hundreds);
    		printf("hundred ");
    	}	
    }
    
    
    
    void thousands(char thousands)
    {
    	if (thousands != '0')
    	{
    		onesplace(thousands);
    	}
    }
    
    
    
    void tenthou(char tenthou)
    {	
    	if (tenthou != '0')
    	{
    		tensplace(tenthou);
    	}
    }
    
    
    
    void hundredthou(char hundredthou)
    {
    	if (hundredthou != '0')
    	{
    		hundreds(hundredthou);
    	}                                       
    }
    
    
    
    void millions(char thousands)
    {
    	if (thousands != '0')
    	{
    		onesplace(thousands);
    	}
    }
    
    
    
    void tenmill(char tenmill)
    {
    	if (tenmill != '0')
    	{
    		tensplace(tenmill);
    	}
    }
    
    
    
    void hundredmill(char hundredmill)
    {
    	if (hundredmill != '0')
    	{
    		hundreds(hundredmill);
    	}          
    	          
    }
    
    
    
    void billions(char billions)
    {
    	if (billions != '0')
    	{
    		onesplace(billions);
    	}
    		
    }
    
    
    
    void tenbill(char tenbill)
    {	
    	if (tenbill != '0')
    	{
    		tensplace(tenbill);
    	}
    }
    
    
    
    void hundredbill(char hundredbill)
    {
    	if (hundredbill != '0')
    	{
    		hundreds(hundredbill);
    	}  	           
    }

  3. #33
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Out of curiosity, why do you have a mix of english and spanish?

  4. #34
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Smile

    actually, my code is in pure english.
    maybe the "spanish" part you're referring to is the name
    of the currency which is "peso". and in our country we use centavos instead of cents.

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To shorten your code, you need to understand the relationship between the column the digit is in, and the word that is associated with it.

    5 = 5 one's
    12 = 1 ten and 2 one's
    148 = 1 hundred forty eight

    1,234,567, or 1,856,723,941, it makes no difference. Each column is associated with a given string:

    Code:
    words[][] = {
    { "and" },   //or blank
    { "one" },
    { "teen"},  //ten is ok, but teen is perhaps better
    { "hundred" }
    { "thousand" },
    //etc.
    };
    You'll need special logic to handle 10, 11, & 12, etc. What you must avoid is handling every digit, in every column, as a "special case", requiring it's own code. You want to abstract the logic enough to cover several general cases, with the same code, in a loop.

    This has all been explained in previous posts, with snippets of code, as well. You may want to re-read some of them for a better understanding.

    I don't know that you can "modify" your code to shorten it - it's a piano you've built, I'm sorry to say. You can't just remove a few keys and say "Look! It's the doghouse I meant to build, at least."

    If you have time, I'd say throw this "piano" away, and try it again, but you may not have that kind of time. In that case, turn it in, and try to understand where you went wrong, for the next assignment.

    I don't believe we can explain the way it should have been done, any better in the next three pages, than we have in the last three pages.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Assembly Tutorials
    By JoshR in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-11-2005, 09:56 AM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM

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