C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Math trunc()

C++ Math trunc()

该函数将给定值四舍五入为零,并返回其幅度不大于给定值的最接近的整数值。
例如:
trunc(3.8) = 3;

语法

假设数字为" x"。语法为:
return_type trunc(data_type x);
注意: return_type可以是float,double或long double。

参数

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

返回值

它返回x的四舍五入值。

示例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

示例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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4