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..?
Comments
:
: 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
:
: 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
http://www.cplusplus.com/ref/cstdio/printf.html
Take Care,
Ed
: : 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.
:
:
:
[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]
**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);