C++ Math trunc()

Last Updated : 21 May 2026

Math trunc() Function

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:

Syntax

It has the following syntax.

Note: return_type can be float,double or long double.

Parameters

x: The value that can be float,double or long double.

Return value

It returns a rounded value of x.

Examples of the List trunc() Function

Here, we are going to discuss several examples to demonstrate the List trunc() Function Function.

Example 1: Truncate a Positive Floating-Point Value Using trunc()

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

Example 2: Truncate a Negative Floating-Point Value Using trunc()

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

Example 3: Use trunc() with an Integer Value

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