C Standard Library

C <math.h> - trunc() Function



The C <math.h> trunc() function is used to round the given number towards zero. It returns the nearest integral value with absolute value less than the argument.

Syntax

double trunc (double x);
float truncf (float x);
long double truncl (long double x);

Parameters

x Specify a number.

Return Value

Returns the nearest integral value with absolute value less than the argument.

Example:

In the example below, trunc() function returns the nearest integral value with absolute value less than the argument.

#include <stdio.h>
#include <math.h>
 
int main (){
  printf("%lf\n", trunc(2.5));
  printf("%lf\n", trunc(5.78));
  printf("%lf\n", trunc(-3.5));
  printf("%lf\n", trunc(-10.33));  
  return 0;
}

The output of the above code will be:

2.000000
5.000000
-3.000000
-10.000000

❮ C <math.h> Library