In C++, the trunc() function is defined in the <cmath> or <math.h> header file and is used to truncate the fractional part of a floating-point number. It rounds the value toward zero and returns the nearest integral value whose magnitude is not greater than the original value.
The trunc() function supports float, double, and long double values. It is commonly used where the decimal part of a number needs to be removed without rounding.
For example:
It has the following syntax.
x: The value that can be float,double or long double.
It returns a rounded value of x.
Here, we are going to discuss several examples to demonstrate the List trunc() Function Function.
This example demonstrates how the trunc() function removes the fractional part of a positive floating-point value.
Output:
The value of x is :8.8 Truncated value of x is :8
This example demonstrates how the trunc() function behaves when the input value is negative.
Output:
The value of x is :-3.9 Truncated value of x is:-3
This example demonstrates how the trunc() function behaves when the input value is already an integer. In this case, the same value is returned.
Output:
The value of x is : 25 Truncated value of x is : 25
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India