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

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

#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  

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

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

网络编程实战 -〔盛延敏〕

性能工程高手课 -〔庄振运〕

NLP实战高手课 -〔王然〕

人人都用得上的写作课 -〔涵柏〕

技术面试官识人手册 -〔熊燚(四火)〕

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

人人都用得上的数字化思维课 -〔付晓岩〕

商业思维案例笔记 -〔曹雄峰〕

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