Thread: Need help to show decimals

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    15

    Need help to show decimals

    I know that I use "float" but where do I place the word float so that my program output shows decimals. Right now the program just cuts the decimals off. This program stores how many of each product you choose to purchase and calculates the total amount paid with tax. The tax is .18 but it's showing only integers, no decimals. Please help me by telling me where to place the word "float" within my program.

    Here's the program:
    Code:
    #include <stdio.h>
    
    	main () {
    
    	int counter,city,shop,i,menu,taxtotal,amounttotal,l,j,k;
    	int breadtotal=0;
    	int diapertotal=0;
    	int beertotal=0;
    	
    	counter=6;
    	shop=1;
    	city=1;
    
    	for (counter>0;counter--;) {
    
    	printf("You are at City # %d and Shop # %d\nPlease enter your product choice\n1 --> Beer\n2 --> Diaper\n3 --> Bread\n0 --> Nothing\n",city,shop);
    	
    	shop=shop+1;
    	
    	if (shop==4) {
    	city=2;
    	shop=1;
    	}
    
    	scanf("%d", &menu);
    	
    		switch (menu) {
    
    		case 1:
    		beertotal=beertotal + 1;
    		break;
    
    		case 2:
    		diapertotal=diapertotal + 1;
    		break;
    
    		case 3:
    		breadtotal=breadtotal + 1;
    		break;
    
    		default: printf("Nothing purchased.\n");
    		}
    			
    
    	}
    	
    	l=beertotal*10;
    	j=diapertotal*5;
    	k=breadtotal*2;
    	taxtotal=(l+j+k)*.18;
    	amounttotal=l+j+k+taxtotal;
    
    	printf("Thank you for shopping with us.\nShopping Summary:\n%d Beer, %d Diapers, %d Bread\nTax paid: $%d\nTotal Amount: $%d\n",beertotal,diapertotal,breadtotal,taxtotal,amounttotal);
    
    	}

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Float is just another variable type. You use it just like int and char. When displaying it, you need &#37;f. To specify the number of decimal places you want, use %.4f for four decimal places. There's also exponential form, but I haven't used that yet.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably you want something like this
    Code:
    int main()
    {
       float a = 0.5;
       double b = 1.23;
       printf("A=&#37;.2f, B= %.2f",a,b);
       return 0;
    }
    Note that main should be declared as int explicetaly
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want taxtotal to be a float, make it so: float taxtotal; instead of int taxtotal; at the top. Same with amounttotal.
    You also have to print floats using (I would suggest in this case) %.2f.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    15
    thank you sirs

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. show() method does not show...
    By kocika73 in forum C++ Programming
    Replies: 12
    Last Post: 04-09-2006, 09:27 PM
  2. how to show the % sign and remove decimals?
    By seal in forum C Programming
    Replies: 5
    Last Post: 08-31-2005, 05:29 PM
  3. good show on PBS tonite
    By axon in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-15-2004, 10:49 PM
  4. That 70's show
    By xds4lx in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-27-2002, 04:16 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM