Reputation: 2087
Тhese are my calculations
float c = 5.0 * (12.0 - 32.0) / 9.0;
printf("%f.2", c);
Result is -11.111111.2 but I expected to be -11.11. What is wrong ?
Upvotes: 1
Views: 106
Reputation: 113
You need to change the Format specification of float; as it is incorrect Instead of using
printf("%f.2", c);
try using this:
float c = 5.0 * (12.0 - 32.0) / 9.0;
printf("%1.2f", c);
this should work! :)
Upvotes: 0
Reputation: 16597
printf("%f.2", c);
should be changed to
printf("%.2f", c);
The update code is as follows:
$ cat f.c
#include <stdio.h>
int main()
{
float c = 5.0 * (12.0 - 32.0) / 9.0;
printf("%.2f \n", c);
return 0;
}
$ gcc f.c
$ ./a.out
-11.11
$
Upvotes: 2
Reputation: 500167
The format specifier should read "%.2f"
.
In what you have right now, the .2
is misplaced. This is why you get more digits than expected, and why the .2
appears verbatim at the end of the output.
Upvotes: 5