December 2005
Beginner to intermediate
618 pages
20h 19m
English
trunc
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.
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
Read now
Unlock full access