function
<cmath> <ctgmath>

# trunc

double trunc  (     double x);
float truncf (      float x);
long double truncl (long double x);
double trunc (     double x);
float trunc (      float x);
long double trunc (long double x);
Truncate value
Rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).

### Parameters

x
Value to truncate.

### Return Value

The nearest integral value that is not larger in magnitude than x (as a floating-point value).

### Example

 1234567891011121314151617 /* round vs floor vs ceil vs trunc */ #include /* printf */ #include /* round, floor, ceil, trunc */ int main () { const char * format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n"; printf ("value\tround\tfloor\tceil\ttrunc\n"); printf ("-----\t-----\t-----\t----\t-----\n"); printf (format, 2.3,round( 2.3),floor( 2.3),ceil( 2.3),trunc( 2.3)); printf (format, 3.8,round( 3.8),floor( 3.8),ceil( 3.8),trunc( 3.8)); printf (format, 5.5,round( 5.5),floor( 5.5),ceil( 5.5),trunc( 5.5)); printf (format,-2.3,round(-2.3),floor(-2.3),ceil(-2.3),trunc(-2.3)); printf (format,-3.8,round(-3.8),floor(-3.8),ceil(-3.8),trunc(-3.8)); printf (format,-5.5,round(-5.5),floor(-5.5),ceil(-5.5),trunc(-5.5)); return 0; }

Output:
 value round floor ceil trunc ----- ----- ----- ---- ----- 2.3 2.0 2.0 3.0 2.0 3.8 4.0 3.0 4.0 3.0 5.5 6.0 5.0 6.0 5.0 -2.3 -2.0 -3.0 -2.0 -2.0 -3.8 -4.0 -4.0 -3.0 -3.0 -5.5 -6.0 -6.0 -5.0 -5.0