C++ std::trunc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::trunc(x) truncates a number, which means it throws away everything after the decimal point. trunc(3.8) is 3 and trunc(-3.8) is -3. It always rounds toward zero.

This is what an (int) cast does, but trunc() keeps the number as a double, so large values do not overflow. For positive numbers it matches floor(), and for negative numbers it matches ceil(). It was added in C++11.

std::trunc() is declared in <cmath> and is part of the group Rounding. Rounding functions turn a fractional number into a whole number. They differ in which way they go for negative numbers and for ties such as 2.5.

Syntax

Include <cmath> and call std::trunc. Start with these lines.

C++
double a = std::trunc(3.8);    // double version
float  b = std::trunc(3.8f);   // float version
CallWhat it does
std::trunc(3.8)Passes a double and returns a double. Here the result is 3.
std::trunc(3.8f)Passes a float, so the float overload runs and returns a float.
std::trunc(3)An integer is converted to double. Returns 3 as a double.

Write std::trunc so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       trunc(float x);
double      trunc(double x);
long double trunc(long double x);
double      trunc(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The number to cut. It can be float, double, long double, or an integer type (converted to double).

Return Value

Return ValueDescription
Truncated valueThe number with its decimal part removed, rounded toward zero, as a floating-point type.
-0.0A negative number between -1 and 0 gives -0.0.
xIf x is already whole, inf, or NaN, it is returned as it is.

C++ Examples

Example 1 — Cut the Decimals Off Several Numbers

Truncate a few numbers. Positive and negative numbers both move toward zero.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {3.8, -3.8, 0.9, -0.9};

    for (double v : values) {
        std::cout << "trunc(" << v << ") = " << std::trunc(v) << '\n';
    }
    return 0;
}

How It Works

  1. The loop gives each number to std::trunc().
  2. 3.8 becomes 3, and -3.8 becomes -3. Both lose their decimals and move toward zero.
  3. 0.9 becomes 0. -0.9 becomes negative zero, so the output shows -0.

Example 2 — trunc(), floor(), and ceil() Side by Side

The three functions agree for whole numbers, but they split apart for decimals, especially negative ones.

C++
#include <cmath>
#include <iostream>

int main() {
    for (double x : {2.7, -2.7}) {
        std::cout << x << ": trunc " << std::trunc(x)
                  << ", floor " << std::floor(x)
                  << ", ceil " << std::ceil(x) << '\n';
    }
    return 0;
}

How It Works

  1. For 2.7, trunc matches floor and gives 2, while ceil goes up to 3.
  2. For -2.7, trunc gives -2 and matches ceil, because both move toward zero.
  3. floor is the only one that gives -3 for -2.7. So trunc() never changes the sign and never grows the size.

Example 3 — Split Seconds into Hours, Minutes, and Seconds

Use trunc() to take the whole part at each step, and keep the rest for the next smaller unit.

C++
#include <cmath>
#include <iostream>

int main() {
    double total = 3725.5;

    double hours = std::trunc(total / 3600.0);
    double rest = total - hours * 3600.0;
    double minutes = std::trunc(rest / 60.0);
    double seconds = rest - minutes * 60.0;

    std::cout << hours << " h " << minutes << " min " << seconds << " s\n";
    return 0;
}

How It Works

  1. 3725.5 / 3600 is about 1.035, and trunc() keeps the 1 hour. The rest is 125.5 seconds.
  2. 125.5 / 60 is about 2.09, so trunc() gives 2 minutes. Those minutes use up 120 seconds.
  3. What is left, 5.5, is the seconds. The program prints 1 h 2 min 5.5 s. Whole values print without a decimal point.

Frequently Asked Questions

std::trunc(x) cuts off the decimal part of x and rounds toward zero. So trunc(3.8) is 3 and trunc(-3.8) is -3.
The number without its fractional part, as a floating-point value. It never moves away from zero, so trunc(-3.8) returns -3.0.
It gives the same number, but trunc() returns a double and cannot overflow for big values. A cast to int gives an int and is undefined if the value is too big.
trunc() always cuts the decimals, so 2.9 becomes 2. round() goes to the nearest whole number, so 2.9 becomes 3.
Include <cmath> and write std::trunc(). That always works and picks the C++ overloads. The global name ::trunc often works too, but the standard does not promise it, and it may only take a double.

Back to the function index

See all 70 <cmath> functions grouped by what they do.

Function index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful