c++ / % 四舍五入 向上取整ceil 向下取整floor
/ % 四舍五入 向上取整ceil 向下取整floor
#include <math.h> double floor(double x);
float floorf(float x);
long double floorl(long double x);
double floor(double x);
double ceil(double x);
使用floor函数。floor(x)返回的是小于或等于x的最大整数。
如: floor(10.5) == 10 floor(-10.5) == -11
使用ceil函数。ceil(x)返回的是大于或等于x的最小整数。
如: ceil(10.5) == 11 ceil(-10.5) ==-10
floor()是向负无穷大舍入,floor(-10.5) == -11;
ceil()是向正无穷大舍入,ceil(-10.5) == -10
1. /
//Test "/"
cout << "Test \"/\"!" << endl;
cout << "7 / 2 = " << 7/2 << endl; //3
cout << "7 / 2.0 = " << 7/2.0 << endl; //3.5
cout << "7.0 / 2 = " << 7.0/2 << endl; //3.5
cout << "7.0 / 2.0 = " << 7.0/2.0 << endl; //3.5
cout << "7 / 3 = " << 7/3 << endl; //2
cout << endl;
2. %
//Test "%"
cout << "Test \"%\"!" << endl;
cout << "9 % 3 = " << 9%3 << endl; //0
cout << "9 % 4 = " << 9%4 << endl; //1
//cout << "9.0 % 3 = " << 9.0%3 << endl;
//cout << "9 % 3.0 = " << 9%3.0 << endl;
cout << endl;
3. 四舍五入
//Test round()
cout << "Test \"四舍五入\"!" << endl;
double dRoundA = 1.4;
double dRoundB = 1.6;
double dRoundLowA = -1.4;
double dRoundLowB = -1.6;
double dRoundLowC = 0.0;
cout << dRoundA << " = " << RoundEx(dRoundA) << endl; //1
cout << dRoundB << " = " << RoundEx(dRoundB) << endl; //2
cout << dRoundLowA << " = " << RoundEx(dRoundLowA) << endl; //-1
cout << dRoundLowB << " = " << RoundEx(dRoundLowB) << endl; //-2
cout << dRoundLowC << " = " << RoundEx(dRoundLowC) << endl; //0
cout << endl;
double RoundEx(const double& dInput)
{
double dIn = dInput;
if (dInput >= 0.0) //???
{
return int(dIn + 0.5);
}
else
{
return int(dIn - 0.5);
}
}
4. ceil() 向上取整
//Test ceil() 向上取整
cout << "Test ceil() 向上取整!" << endl;
cout << "ceil 1.2 = " << ceil(1.2) << endl; //2
cout << "ceil 1.8 = " << ceil(1.8) << endl; //2
cout << "ceil -1.2 = " << ceil(-1.2) << endl; //-1
cout << "ceil -1.8 = " << ceil(-1.8) << endl; //-1
cout << "ceil 0.0 = " << ceil(0.0) << endl; //0
cout << endl;
5. floor() 向下取整
//Test floor() 向下取整
cout << "Test floor() 向下取整!" << endl;
cout << "floor 1.2 = " << floor(1.2) << endl; //1
cout << "floor 1.8 = " << floor(1.8) << endl; //1
cout << "floor -1.2 = " << floor(-1.2) << endl; //-2
cout << "floor -1.8 = " << floor(-1.8) << endl; //-2
cout << "floor 0.0 = " << floor(0.0) << endl; //0
cout << endl;
c++ / % 四舍五入 向上取整ceil 向下取整floor的更多相关文章
- python(50):python 向上取整 ceil 向下取整 floor 四舍五入 round
取整:ceil 向下取整:floor 四舍五入:round 使用如下:
- python 向上取整ceil 向下取整floor 四舍五入round
#encoding:utf-8 import math #向上取整 http://www.manongjc.com/article/1335.html print "math.ceil--- ...
- js 向上取整、向下取整、四舍五入
js 向上取整.向下取整.四舍五入 CreateTime--2018年4月14日11:31:21 Author:Marydon // 1.只保留整数部分(丢弃小数部分) parseInt(5.12 ...
- Oracle四舍五入,向上取整,向下取整
用oracle sql对数字进行操作: 取上取整.向下取整.保留N位小数.四舍五入.数字格式化 取整(向下取整): select floor(5.534) from dual; select trun ...
- c# 小数四舍五入,向上取整,向下取整,见角进元保留多个小数位数
/// <summary> /// 实现数据的四舍五入法 /// </summary> /// <param name="v">要进行处理的数据 ...
- Oracle - 数字处理 - 取上取整、向下取整、保留N位小数、四舍五入、数字格式化
用oracle sql对数字进行操作: 取上取整.向下取整.保留N位小数.四舍五入.数字格式化 取整(向下取整): select floor(5.534) from dual; select trun ...
- PHP取整,四舍五入取整、向上取整、向下取整、小数截取
PHP取整数函数常用的四种方法: 1.直接取整,舍弃小数,保留整数:intval(): 2.四舍五入取整:round(): 3.向上取整,有小数就加1:ceil(): 4.向下取整:floor(). ...
- Python 之 向上取整、向下取整以及四舍五入函数
import math f = 11.2 print math.ceil(f) #向上取整 print math.floor(f) #向下取整 print round(f) #四舍五入 #这三个函数的 ...
- JQ向上取整 和向下取整 四舍五入
向上取整 var a = 23.2325236 var abc = Math.ceil(a); //注意:Math.ceil(a)不要单独写一行,否则向上取整失败 abc = 24; ...
随机推荐
- HBuilderX 打包
新建 - 云打包 (密钥 密码看不到 - 回车) ( ) BlueStacks蓝叠 模拟器看效果
- Shiro学习笔记总结,附加" 身份认证 "源码案例(一)
Shiro学习笔记总结 内容介绍: 一.Shiro介绍 二.subject认证主体 三.身份认证流程 四.Realm & JDBC reaml介绍 五.Shiro.ini配置介绍 六.源码案例 ...
- 解决国内安装tensorflow, opencv等安装不成功或下载太慢问题
解决国内安装tensorflow, opencv等安装不成功或下载太慢问题 复制自博客:https://blog.csdn.net/jorg_zhao/article/details/80075293 ...
- ios h5 长按时出现黑色透明遮罩
html,body{-webkit-text-size-adjust: 100%;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);}
- QTP(5)
一.检查点 1.位图检查点(Bitmap CheckPoint) (1)作用:主要用于检查UI界面,检查页面布局,包括控件位置.大小.颜色.状态等 (2)确定位图检查点的要素: a.检查哪个控件 b. ...
- HH的项链 HYSBZ - 1878 (莫队/ 树状数组)
HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此他的项链变得越来越长.有一天,他突然 ...
- 【CF335 E】Counting Skyscrapers
题意 有一排高楼,每一栋高楼有一个正整数高度,高度为 \(i\) 的概率为 \(2^{-i}\).一栋楼的每层从下往上依次编号为 \(0,1,2,\cdots,i-1\). 为了出题,大楼之间安装了溜 ...
- winform Combox绑定数据时不触发SelectIndexChanged事件
做了一个仓库选择的联动,选了仓库选其下的货区,选了货区选其下的货架分区.每个combox初始化.绑定数据是都会触发SelectIndexChanged事件,相当头疼. 后来无意中在网上看到了一种方法— ...
- Java常用类(二) Scanner类和大数类
二.Scanner类 有C系语言基础的可能都比较熟悉scanf("%d",&a);和cin>>a;这种代码,也打开了程序交互的第一道门.因此,这些程序员开始学J ...
- 2019CCPC秦皇岛(重现赛)-D
链接: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=872 题意: 给定一个正整数 n,要求判断 1 ...