Name
trunc
Synopsis
Rounds a floating-point number toward 0 to an integer value
#include <math.h> doubletrunc
( doublex
); floattruncf
( floatx
); long doubletruncl
( long doublex
);
The trunc()
functions round
the value of their argument, x
, to the
nearest integer value whose magnitude is not greater than that of
x
—in 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
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.