set precision of float in C
To set the precision of a float in C, you can use the printf
function with the %f
format specifier and the %.nf
precision specifier, where n
is the desired number of decimal places. Here's an example:
#include <stdio.h>
int main() {
float number = 3.14159;
printf("%.2f\n", number); // Sets precision to 2 decimal places
return 0;
}
In this example, the output will be 3.14
, as the precision is set to 2 decimal places. You can change the value of n
to set the desired precision.