# Displaying numbers to two decimal places using printf

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-25-2006
kie
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
• 10-25-2006
Salem
I think %g does trailing zero suppression.

Though if you also want say 21.00, then you'll need another approach.
• 10-25-2006
sangken
will
Code:

printf("£%.2f is equal to %.2f euros.", input, output);
work??
• 10-25-2006
kris.c
by currency format , U mean , U need two digits after the decimal right? I think sangken is right..
• 10-25-2006
kie
that has worked perfectly

• 10-25-2006
kie
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

• 10-25-2006
kris.c
U can consider using
" isdigit() " to do that..
Check the man pages
• 10-25-2006
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 ;-).
• 10-25-2006
kie
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
{
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-25-2006
swoopy
>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.
• 10-25-2006
kie
i thought it may have been because it's a float.

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

thanks
• 10-25-2006
swoopy
>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);

• 10-25-2006
Dave_Sinkula
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.
• 10-25-2006
swoopy
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.
• 10-25-2006
Dave_Sinkula
Maybe a bit ahead of the game, but possibly something to look at:
http://www.daniweb.com/code/snippet258.html
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last