Previous Page
Next Page

trunc

Rounds a floating-point number toward 0 to an integer value

#include <math.h>
double trunc ( double x  );
float truncf ( float x  );
long double truncl ( long double x  );

The TRunc( ) functions round the value of their argument, x, to the nearest integer value whose magnitude is not greater than that of xin other words, toward 0.

Example

printf("trunc(-1.7) = %.2f   trunc(1.4) = %.2f   trunc(1.5) = %.2f\n",
        trunc(-1.7),  trunc(1.4), trunc(1.5) );

printf("round(-1.7) = %.2f   round(1.4) = %.2f   round(1.5) = %.2f\n",
        round(-1.7), round(1.4), round(1.5) );

This code produces the following output:

trunc(-1.7) = -1.00   trunc(1.4) = 1.00   trunc(1.5) = 1.00
round(-1.7) = -2.00   round(1.4) = 1.00   round(1.5) = 2.00

See Also

rint( ), lrint( ), llrint( ), round( ), lround( ), llround( ), nearbyint( )


Previous Page
Next Page