Home > Article > Backend Development > Write a one-line C function to round floating point numbers
Here we will see how to write a one line C function that can round floating point numbers. In order to solve this problem, we have to follow the following steps.
#include <stdio.h> int my_round(float number) { return (int) (number < 0 ? number - 0.5 : number + 0.5); } int main () { printf("Rounding of (2.48): %d</p><p>", my_round(2.48)); printf("Rounding of (-5.79): %d</p><p>",my_round(-5.79)); }
Rounding of (2.48): 2 Rounding of (-5.79): -6
The above is the detailed content of Write a one-line C function to round floating point numbers. For more information, please follow other related articles on the PHP Chinese website!