+ 1

In c While writing float values sometimes i have to take %3.2f or sometimes %.3f or sometimes %.2f or sometimes only %f why ???

9th Nov 2018, 1:56 PM
Lata Mishra
Lata Mishra - avatar
4 ответов
+ 8
%f means it will print the float value with no gap %2f means it will give gap of 2 spaces then print the value %3.2f In this printf statement we want to print three position before the decimal point (called width) and two positions behind the decimal point (called precision). its just like round the precision value https://code.sololearn.com/cyDYZaAh4lDj/?ref=app
9th Nov 2018, 2:27 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 4
%f is giving 3.433333 %.2f is giving 3.43 so the value after (.) Is reduce to length 2 in above example so how you getting same output? Lata Mishra
9th Nov 2018, 2:49 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 2
Without examples in working code, it would only be guess work on our part to answer you. The only reason to specify the 3 in %3.2f is to be sure you get at least 3 columns and, with that 2 it is guarenteed as it requires a decimal point plus 2 digits following it.
9th Nov 2018, 2:30 PM
John Wells
John Wells - avatar
0
GAWEN after looking at your code i changed some values printf ("%f\n",d); printf ("%.2f\n",d); But Still i am getting same output why??
9th Nov 2018, 2:36 PM
Lata Mishra
Lata Mishra - avatar