I have a piece of code that looks like this:
float nb = 100 / 42;
printf("%.2f", nb);
which I expect to print out 2.38
, but instead it prints out 2.00
.
The 42
is just an example. In the original code it's a variable.
I have a piece of code that looks like this:
float nb = 100 / 42;
printf("%.2f", nb);
which I expect to print out 2.38
, but instead it prints out 2.00
.
The 42
is just an example. In the original code it's a variable.
You need to specify the numbers as floating point themselves. Try this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
float nb = 100.0/42.0;
printf("%.2f\n", nb);
return 0;
}