In this C++ tutorial, you will learn how to truncate the decimal part of given number using trunc() function of cmath, with syntax and examples.

C++ trunc()

C++ trunc() truncates the decimal part of given number (argument).

In terms of rounding, trunc() function rounds the number towards zero and returns the nearest integral value.


The syntax of C++ trunc() is



xA double, float, or long double value.


The return value depends on the type of value passed for parameter x.

The return value of trunc(x) is

  • double if x is double.
  • float if x is float.
  • long double if x is long double.

The synopsis of trunc() function is

double trunc(double x);
float trunc(float x);
long double trunc(long double x);
double trunc(T x); // for integral type argument values

trunc() is a function of cmath library. Include cmath library in the program, if using trunc() function.



In this example, we read a double value from user into x, and find its truncated value using trunc() function.

C++ Program

#include <iostream>
using namespace std;

int main() {
    double x;
    cout << "Enter a number : ";
    cin >> x;
    double result = trunc(x);
    cout << "trunc(" << x << ") : " << result << endl;


Enter a number : 3.14
trunc(3.14) : 3
Program ended with exit code: 0
Enter a number : -3.14
trunc(-3.14) : -3
Program ended with exit code: 0
Enter a number : -0.21
trunc(-0.21) : -0
Program ended with exit code: 0


In this C++ Tutorial, we learned the syntax of C++ trunc(), and how to use this function to find the truncated value of given number, with the help of examples.