Math Trunc Function

C++ Math trunc

The function rounds off the given value towards zero and returns the nearest integral value whose magnitude is not greater than the given value.

For example:

Example

trunc(3.8) = 3;

Syntax

Suppose a number is 'x'. Syntax would be :

Example

return_type trunc(data_type x);

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

Parameter

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

Return value

It returns a rounded value of x.

Example 1

Let's see a simple example when the value of x is positive.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    float x=8.8;
    std::cout << "The value of x is :" <<x<< std::endl;
    std::cout << "Truncated value of x is :" <<trunc(x)<< std::endl;
    return 0;
}

Output:

Output

The value of x is :8.8
Truncated value of x is :8

Example 2

Let's see a simple example when the value of x is negative.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    double x=-3.9;
    std::cout << "The value of x is :" <<x<< std::endl;
    std::cout << "Truncated value of x is:" <<trunc(x)<< std::endl;
    return 0;
}

Output:

Output

The value of x is :-3.9
Truncated value of x is:-3

Input Required

This code uses input(). Please provide values below: