English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
double trunc(double x); float trunc(float x); long double trunc(long double x); double trunc(T x); //integer type
The trunc() function takes a single parameter and returns a value of type double, float, or long double. This function is in<cmath>defined in the header file.
The trunc() function takes a single parameter, whose trunc value will be calculated.
In short, the trunc() function truncates the decimal part and returns only the integer part.
#include <iostream> #include <cmath> using namespace std; int main() { double x = 10.25, result; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; x = -34.251; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; return 0; }
When the program is executed, the output is:
trunc(10.25) = 10 trunc(-34.251) = -34
#include <iostream> #include <cmath> using namespace std; int main() { int x = 15; double result; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; return 0; }
When the program is executed, the output is:
trunc(15) = 15
For integer values, the trunc function will return the same result. Therefore, it is not commonly used to represent integer values in practice.