无涯教程-trunc(x)函数

39 阅读1分钟

函数将给定值四舍五入为零,并返回最接近的整数值,其大小不大于给定值。

例如:

trunc(3.8) = 3;

trunc - 语法

假设数字是“ x”。语法为:

return_type trunc(data_type x);

Note: return_type可以是float,double或long double。

trunc - 参数

x :可以是float,double或long double的值。

trunc - 返回值

它返回x的舍入值。

trunc - 例子1

让我们看一个简单的例子,当x的值为正时。

#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;  
}  

输出:

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

trunc - 例子2

让我们看一个简单的例子,当x的值为负数时。

#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;  
}  

输出:

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

参考链接

www.learnfk.com/c++/cpp-mat…