floor(x)函数详解

首页 / C++入门教程 / floor(x)函数详解

它将值舍入到不大于给定值的最接近的整数。

例如:

floor(8.2)=8.0;
floor(-8.8)=-9.0;

floor - 语法

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

double floor(double x);

floor - 参数

x : 四舍五入到最接近的整数的值。

floor - 返回值

它返回舍入到不大于x的最接近整数的值。

floor - 例子1

让我们看一个简单的例子,考虑正值。

#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   

floor - 例子2

让我们看一个简单的例子,考虑负值。

链接: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  

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构核心20讲 -〔杨波〕

高并发系统设计40问 -〔唐扬〕

Electron开发实战 -〔邓耀龙〕

互联网人的英语私教课 -〔陈亦峰〕

etcd实战课 -〔唐聪〕

Spark性能调优实战 -〔吴磊〕

超级访谈:对话张雪峰 -〔张雪峰〕

大厂广告产品心法 -〔郭谊〕

Web 3.0入局攻略 -〔郭大治〕

好记忆不如烂笔头。留下您的足迹吧 :)