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
Call
What 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
Parameter
Description
x
Required. The number to cut. It can be float, double, long double, or an integer type (converted to double).
Return Value
Return Value
Description
Truncated value
The number with its decimal part removed, rounded toward zero, as a floating-point type.
-0.0
A negative number between -1 and 0 gives -0.0.
x
If 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;
}
3725.5 / 3600 is about 1.035, and trunc() keeps the 1 hour. The rest is 125.5 seconds.
125.5 / 60 is about 2.09, so trunc() gives 2 minutes. Those minutes use up 120 seconds.
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.