round float in C
To round a float in C, you can use the round()
function from the math.h
library. Here's an example:
#include <stdio.h>
#include <math.h>
int main() {
float num = 3.7;
int rounded = round(num);
printf("Rounded value: %d\n", rounded);
return 0;
}
This program declares a float variable num
with the value 3.7. It then uses the round()
function to round num
to the nearest integer. The rounded value is stored in an integer variable rounded
, which is then printed to the console.
Keep in mind that the round()
function returns a double
value, so you may need to cast it to an int
if you want to store it as an integer.
I hope this helps! Let me know if you have any further questions.