它将值舍入到不大于给定值的最接近的整数。
例如:
floor(8.2)=8.0; floor(-8.8)=-9.0;
假设数字是“ x”。语法为:
double floor(double x);
x : 四舍五入到最接近的整数的值。
它返回舍入到不大于x的最接近整数的值。
让我们看一个简单的例子,考虑正值。
#include <iostream> #include<math.h> using namespace std; int main() { float x=7.8; std::cout << "Initial value of x is : " << x<<std::endl; cout<<"Now, the value of x is :"<<floor(x); return 0; }
输出:
Initial value of x is : 7.8 Now, the value of x is :7
让我们看一个简单的例子,考虑负值。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-math-floor-function.html
来源:LearnFk无涯教程网
#include <iostream> #include<math.h> using namespace std; int main() { float x=-10.2; std::cout << "Initial value of x is : " << x<<std::endl; cout<<"Now, the value of x is :"<<floor(x); return 0; }
输出:
Initial value of x is : -10.2 Now, the value of x is :-11
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)