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; ...
随机推荐
- OO方式实现ALV: cl_salv_table
这里总结最近用cl_salv_table实现ALV遇到问题和解决办法 FORM set_alv2 . DATA: lv_syrepid TYPE syrepid. lv_syrepid = sy-cp ...
- Sublime Text 3:自定义语法高亮
(http://ilkinulas.github.io/programming/2016/02/05/sublime-text-syntax-highlighting.html) 要安装"P ...
- Eclispe造成的tomcat占用端口 无法启动 强制终止进程 转载
很多时候运行tomcat 的时候总是会提示tomcat 的端口被占用 但是任务管理器里面还找不到是哪个端口被占用了 因此很多人就重新配置tomcat 或者去修改tomcat的端口号 ,其实这么做太麻 ...
- Visual Studio 2019社区版:错误 MSB6006 “CL.exe”已退出,代码为 2
系统:win10 环境:Visual Studio 2019社区版 问题:错误 MSB6006 “CL.exe”已退出,代码为 2 解决方法: 1 一个类内部的定义返回类型为double的方法种没有写 ...
- MyBatis 报错org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource
报错如下: org.apache.ibatis.exceptions.PersistenceException: ### Error opening session. Cause: java.lang ...
- Django学习系列21:为每一个清单添加唯一URL
现在让我们来解决我们真正的问题,即我们的设计只允许一个全局列表. 我将演示一个关键的TDD技术:如何使用一个渐进的.循序渐进的过程来适应现有的代码,这些过程将您从工作状态转移到工作状态.测试山羊,而不 ...
- Python:面向对象编程2
types.MethodType __slot__ @property, @xxx.setter Python的多重继承和MinIn 如何在class创建后,给实例绑定属性和方法? (动态绑定/定义 ...
- ffmpeg 在vs配置的库名称
avcodec.lib; avformat.lib; avutil.lib; avdevice.lib; avfilter.lib;postproc.lib;swresample.lib; swsca ...
- 开源的任务系统 TaskManager
Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...
- web上传整个文件夹
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...