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; ...
随机推荐
- 解决 /etc/init.d/php-fpm no such file or directory等相关问题
vi /etc/init.d/php-fpm #! /bin/sh # Comments to support chkconfig on CentOS # chkconfig: 2345 65 37 ...
- pam_smb
What is pam_smb? pam_smb is a PAM module/server which allows authentication of UNIX users using an N ...
- deep_learning_Function_tensorboard的使用
数据可视化(网页能打开,但是没有数据):https://jingyan.baidu.com/article/e9fb46e1c55ac93520f7666b.html
- Android Studio Terminal 常用命令
一.Manifest merger failed with multiple errors, see logs 在android开发的时候,有时候会遇到这样的问题 Error:Execution fa ...
- 爱搞事情的webpack
webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler). 当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(dependency g ...
- Linux-Shell编程之创建shell脚本并执行
1.创建文件 touch myFirst.sh 2.编辑文件 vi myFirst.sh 点击键盘的字母 I 进行编辑,输入一下内容 #!/bin/bash # My First script ls ...
- ArcEngine打开本地数据库
先看一下GeoDatabase核心结构模型图: 1 工作空间工厂WorkspaceFactory对象 WorkspaceFactory是GeoDatabase的入口,是一个抽象类,拥有很多子类,例如 ...
- monkey test——学习资料
出处: http://www.testwo.com/blog/6107 http://www.testwo.com/blog/6146 http://www.testwo.com/blog/6188 ...
- bat脚本休眠时间技巧
例子: @echo off set /a i=1 :c adb connect 192.168.1.109&ping -n 1 123.45.67.89 -w 10000>nul if ...
- Confluence 6 分享一个文件
协同合作和编辑不仅仅是发生在页面中,很多时候你需要与你的项目小组针对文档,报告,图片,表格进行协同操作.不管是针对性的市场计划或者一个完整的项目计划,你可以在 Confluence 中让你的项目小组成 ...