469,889 Members | 1,352 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

how to cut off the decimal?

printf("%lf",3.25);

the result is 3.25000

i want the answer correct to 3 decimal places

What should i do?
thanks!
Nov 15 '05 #1
8 10659
"nick" <i1********@yahoo.com>wrote:
printf("%lf",3.25);
the result is 3.25000
i want the answer correct to 3 decimal places
What should i do?
thanks!


use: .n after % and bevour f
n decimal places.

printf("%.3lf", 3,25);
^^
the result is 3.250;


Nov 15 '05 #2

Eberhard Funck wrote:

printf("%.3lf", 3,25);
^^
the result is 3.250;

I guess you thought of
printf("%.3lf", 3.25);
otherwise the result is 3.000

regards
Michael

Nov 15 '05 #3

"Michael" <mi****@gmx.net> schrieb:

Eberhard Funck wrote:

printf("%.3lf", 3,25);
^^
the result is 3.250;
I guess you thought of
printf("%.3lf", 3.25);

^ otherwise the result is 3.000

regards
Michael


Hello Michael,

Thanks for the hint.
It was my mistake with "," instead of ".".

Thanks.
Nov 15 '05 #4


Eberhard Funck wrote:
printf("%.3lf", 3,25);

[...] the result is 3.000


Hi Eberhard,
well I'm not sure if I am right with the above
statement, since the types do not match (int vs double).
Is this converted during compiling or pure luck if 3.000
is shown?
Any hints?

Thanks,
Michael

Nov 15 '05 #5

"Michael" wrote:


Eberhard Funck wrote:
printf("%.3lf", 3,25);
[...] the result is 3.000


Hi Eberhard,
well I'm not sure if I am right with the above
statement, since the types do not match (int vs double).
Is this converted during compiling or pure luck if 3.000
is shown?
Any hints?

Thanks,
Michael


Hi Michael,

I think it's pure luck.
My lcc compiler makes this warning, if I write 3,25 instead 3.25:
"Warning printf argument mismatch for format f. Expected double got int"
The result is 0.000

Eberhard

Nov 15 '05 #6
nick wrote:
printf("%lf",3.25);

the result is 3.25000

i want the answer correct to 3 decimal places

What should i do?
Short answer: open an elementary C textbook.
Spoon-fed longer answer:

#include <stdio.h>

void show(double x)
{
/* note that the specifier is %f or %g, not %lf or %lg */
printf("total precision of 3 decimal places:\n %.3g\n", x);
printf("3 decimal places to right of decimal:\n %.3f\n\n", x);
}

int main(void)
{
show(3.25);
show(325);
show(.325);
show(3.2574);
show(3.2547);
return 0;
}
total precision of 3 decimal places:
3.25
3 decimal places to right of decimal:
3.250

total precision of 3 decimal places:
325
3 decimal places to right of decimal:
325.000

total precision of 3 decimal places:
0.325
3 decimal places to right of decimal:
0.325

total precision of 3 decimal places:
3.26
3 decimal places to right of decimal:
3.257

total precision of 3 decimal places:
3.25
3 decimal places to right of decimal:
3.255

thanks!

Nov 15 '05 #7
Eberhard Funck wrote:
"nick" <i1********@yahoo.com>wrote:
printf("%lf",3.25);

printf("%.3lf", 3,25);


Both of you need to know: the printf specifier for a double (or float)
is "%f".
Nov 15 '05 #8
>> printf("%.3lf", 3,25);

Both of you need to know: the printf specifier for a double (or float)
is "%f".


Thanks for correction.
Regards
Michael

Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Batista, Facundo | last post: by
4 posts views Thread by Julian Hernandez Gomez | last post: by
11 posts views Thread by Timothy Smith | last post: by
2 posts views Thread by Steve Summit | last post: by
10 posts views Thread by Paul Sullivan | last post: by
25 posts views Thread by Lennart Benschop | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.