C++ Math round()
C++ Math round()
此函数用于舍入给定值,该值可以是float或double。
例如:
round(5.8)= 6;
round(-1.1)=-1;
语法
假设数字为" x"。语法为:
return_type round(data_type x);
参数
x : 该值可以是float或double。
返回值
它返回x的舍入值。值的返回类型可以是float,double或long double。
示例1
让我们看一个简单的示例,当x的值为正时
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=8.3;
std::cout << "The value of x is : " <<x<< std::endl;
cout<<"Rounded value of x is : "<<round(x);
return 0;
}
输出:
The value of x is : 8.3
Rounded value of x is : 8
示例2
让我们看一个简单的示例,当x的值为负时。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double x=-9.9;
std::cout << "The value of x is : " <<x<< std::endl;
cout<<"Rounded value of x is : "<<round(x);
return 0;
}
输出:
The value of x is :-9.9
Rounded value of x is :-10