The use of.....%2f

Hi !

I started studying towards to the final exam!!! That's going to be on a week Thursday sitting 3hrs in the classroom...

Anyway, the outout of using %f or %2f (pr %1f, %3f...) doesn't seem to be different but the use of numbers in between % and f(or d or whatever) is introduced without the explanation and I wondered why..?

float number = 2.22;

printf("Print in normal style: %f
", number);
printf("Print with 2 in it: %2f
", number);
printf("Print with 3 in it: %3f
", number);

All above outputs exactly the same!

Do you know why..?

• : Hi !
:
: I started studying towards to the final exam!!! That's going to be on a week Thursday sitting 3hrs in the classroom...
:
: Anyway, the outout of using %f or %2f (pr %1f, %3f...) doesn't seem to be different but the use of numbers in between % and f(or d or whatever) is introduced without the explanation and I wondered why..?
:
: float number = 2.22;
:
: printf("Print in normal style: %f
", number);
: printf("Print with 2 in it: %2f
", number);
: printf("Print with 3 in it: %3f
", number);
:
: All above outputs exactly the same!
:
: Do you know why..?
:
It shouldn't be the same:
[code]
[italic]Note the blanks before the number[/italic]
%f should print:
2.22

%2f should print:
2.22

%3f should print:
2.22

%3.5f should print:
2.22000
[/code]
I didn't try it, but that's how it should be.

Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl

• [b][red]This message was edited by stober at 2006-8-4 6:23:14[/red][/b][hr]
:
: printf("Print in normal style: %f
", number);
: printf("Print with 2 in it: %[red].2f[/red], number);
: printf("Print with 3 in it: %[red].3f[/red]
", number);
:
:

If you want the output to contain a different number of decmal places then you must put a decimal point between % and the number

• You've gotten a couple of specific answers, but I thought I'd add a link that gives a pretty good description of all the parameters for printf:

http://www.cplusplus.com/ref/cstdio/printf.html

Take Care,
Ed

• : : Hi !
: : All above outputs exactly the same!
: : Do you know why..?
As far as I know, the number in front of the dot tells the entire space in characters, not only the leading digits in front of the decimal. As the display of 2.22 needs 4 characters, 2 or 3 are not enough and therefore extended.

• Cheers for the replies. I know the decimal point and its location makes it diferent on printing it out with printf but without decimal point didnt make any difference on my computer yet it's introduced in couple of codes I've seen and tried but didn't see any difference from not placing any numbers in front without decimal so... I wondered why and what is going on...

• : Cheers for the replies. I know the decimal point and its location makes it diferent on printing it out with printf but without decimal point didnt make any difference on my computer yet it's introduced in couple of codes I've seen and tried but didn't see any difference from not placing any numbers in front without decimal so... I wondered why and what is going on...
:
:
:
[blue]Had you noted the reply by FDrache? That's why there was no difference printing 2.22 until you get up to %5f. The leading number is the total character spaces taken up by all the digits before and after (plus) the decimal point, whereas the number after the decimal point is only the number of digits to print after the point (the precision).

Take Care,
Ed[/blue]

• Philippines

**Hi !

I started studying towards to the final exam!!! That's going to be on a week Thursday sitting 3hrs in the classroom...

Anyway, the outout of using %f or %2f (pr %1f, %3f...) doesn't seem to be different but the use of numbers in between % and f(or d or whatever) is introduced without the explanation and I wondered why..?

float number = 2.22;

printf("Print in normal style: %f
", number);
printf("Print with 2 in it: %2f
", number);
printf("Print with 3 in it: %3f
", number);

All above outputs exactly the same!

Do you know why..?
**

you we're just printing another number beside the input number..

+Another one..
if you want to have an exact 2 decimals places, the syntax should be

printf("%.2f", number);