Displaying numbers to two decimal places using printf

This is a discussion on Displaying numbers to two decimal places using printf within the C Programming forums, part of the General Programming Boards category; Hey I am currently in uni and I have just started to learn basic C. For a project, I have ...

  1. #1
    kie
    kie is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    Question Displaying numbers to two decimal places using printf

    Hey

    I am currently in uni and I have just started to learn basic C. For a project, I have to make a simple application that can convert GBP into euros (roughly). The problem I have is displaying the input and the output as a decimal number.

    Here is my code:

    Code:
    /*	Currency conversion app
    	  	 Kieran Symes       */
    
    #include <stdio.h>
    
    void welcome(void); //displays welcome message and app info
    void convert(void); //converts users input to $
    
    
    int main()
    {
    
    	welcome();
       convert();
    
    	return 0;
    }
    
    //this bit welcomes the user and shows some info
    void welcome(void)
    {
    	printf ("Currency Converter\nBy Kieran Symes\n24/10/2006\n\n");
    
    }
    
    void convert(void)
    {
    
    float input;
    float output;
    
    	printf("Please enter an amount you wish to convert\n");
       scanf("%f", &input);
    
       output = input * 1.5;
    
       printf("£%f is equal to %f euros.", input, output);
    
    }
    The output for this, say if I enter 21 is

    £21.000000 is equal to 31.500000 euros. but I want these figures to be displayed in currency format.

    Any help and comments will be appreciated. Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    I think %g does trailing zero suppression.

    Though if you also want say 21.00, then you'll need another approach.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    will
    Code:
    printf("£%.2f is equal to %.2f euros.", input, output);
    work??
    It is not who I am inside but what I do that defines me.

  4. #4
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    by currency format , U mean , U need two digits after the decimal right? I think sangken is right..
    In the middle of difficulty, lies opportunity

  5. #5
    kie
    kie is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    that has worked perfectly

    thanks for your help

  6. #6
    kie
    kie is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    so could you help me validate the data to ensure it is a number?

    i guess an if statement would be best although I don't know what to analyze data obtained from scanf.

    I guess it would be an if statement like this...

    if the data is numeric
    go to the convert fucntion
    else
    print message warning
    loop back to input function

    I have to include a few functions see

    thanks again for your help

  7. #7
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    U can consider using
    " isdigit() " to do that..
    Check the man pages
    In the middle of difficulty, lies opportunity

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    You COULD of course go into checking values and all such things, but funny thing:
    scanf(); only accepts input that fits into the format string!

    Code:
    scanf("%.2f", FloatNum);
    Only accepts a number that fits into a float of the format #.?? (ie 12.64 or 53.00)
    checking to see if FloatNum is indeed a float and getting an error from that would be a marvellous discovery and do please document it well ;-).

  9. #9
    kie
    kie is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    i am trying the following code within the convert function

    Code:
    void convert(void)
    {
    float input;
    float output;
    	printf("Please enter an amount you wish to convert\n");
       scanf("%f", &input);
    	if(isdigit(input))  //prblem code
       {
       output = input * 1.49;
       printf("£%.2f is equal to %.2f euros.", input, output);
       }
       else
       {
       printf("There was an error with your entry.  Please try again\n")
       convert();
       }
    
    
    }
    i have had a look into isdigit() and it looks like what I need although I am unable to get it to work.

    does it look like I have used it right?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >does it look like I have used it right?
    No, isdigit takes an int/char. You're passing it a float (float input).

    If you want to validate with isdigit, first use fgets to read into a string, then use strtod to convert to a float or double after checking for valid input. Or you could do the validation with strtod.

  11. #11
    kie
    kie is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    i thought it may have been because it's a float.

    how would I use strtod? could you give me some sample code?

    thanks

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >could you give me some sample code?
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char line[80];
    	double amount;
    	char *endptr;
    	int valid = 0;
    
    	do {
    		printf("Please enter an amount you wish to convert: ");
    		fgets(line, 80, stdin);
    		amount = strtod(line, &endptr);
    		if (endptr == line)
    		{
    			printf("Invalid input.\n");
    			valid = 0;
    		}
    		else
    		{
    			valid = 1;
    		}
    	} while (!valid);
    
    	printf("Amount: %.2f\n", amount);
    Last edited by swoopy; 10-25-2006 at 04:55 PM.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Asmyldof
    You COULD of course go into checking values and all such things, but funny thing:
    scanf(); only accepts input that fits into the format string!

    Code:
    scanf("%.2f", FloatNum);
    Only accepts a number that fits into a float of the format #.?? (ie 12.64 or 53.00)
    checking to see if FloatNum is indeed a float and getting an error from that would be a marvellous discovery and do please document it well ;-).
    The highlighted material is incorrect.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You can also use sscanf in place of strtod if you prefer:
    Code:
          if (sscanf(line, "%f", &amount) != 1)
          {
             printf("Invalid input.\n");
             valid = 0;
          }
    Then you don't need endptr.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Maybe a bit ahead of the game, but possibly something to look at:
    http://www.daniweb.com/code/snippet258.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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

Similar Threads

  1. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 08:44 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. %g doesn't print as many decimal places as %f
    By octoc in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 PM
  4. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10:03 AM
  5. whats wrong with this?
    By petedee in forum C Programming
    Replies: 32
    Last Post: 01-06-2004, 09:28 PM

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