C++ Standard Library C++ STL Library

C++ <cmath> - trunc() Function



The C++ <cmath> 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 trunc (float x);
long double trunc(long double x);
double trunc (T 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 <iostream>
#include <cmath>
using namespace std;
 
int main (){
  cout<<trunc(2.5)<<"\n";
  cout<<trunc(5.78)<<"\n";
  cout<<trunc(-3.5)<<"\n";
  cout<<trunc(-10.33)<<"\n";  
  return 0;
}

The output of the above code will be:

2
5
-3
-10

❮ C++ <cmath> Library