取整函数(向上取整向下取整四舍五⼊)
取整函数(在#include<math.h>之下)
Floor() 会取不⼤于⾃变量的最⼤整数,这样⾃变量是3.1或3.9是没有区别的,返回都是3;⾃变量是-2.1或-2.9也是没有区别的,返回都是-3;(向下取整)
#include<iostream>
#include<algorithm>
#include<map>
#include<math.h>
#include<string>
using namespace std;
int main(){
cout<<floor(3.9);// 输出3
}
Ceil() 会取不⼩于⾃变量的最⼤整数,这样⾃变量是3.1或3.9,返回都是4;⾃变量是-2.1或-2.9,返回的都是-2;(向上取整)
#include<iostream>
#include<algorithm>
#include<map>
#include<math.h>
#include<string>
using namespace std;
int main(){
cout<<ceil(3.1);// 输出4
}
Round() 函数,才是我们需要的四舍五⼊的函数,因为它会返回离⾃变量最近的整数,这个返回的整数可能⼤于也可能⼩于原来的数,但是⼀定是离它最近的那个整数。(四舍五⼊)
四舍五入函数保留整数#include<iostream>
#include<algorithm>
#include<map>
#include<math.h>
#include<string>
using namespace std;
int main(){
cout<<round(3.5);// 输出4
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。