Python 向上取整的算法
一、初衷:
有时候我们分页展示数据的时候,需要计算页数。一般都是向上取整,例如counts=205 pageCouts=20 ,pages= 11 页。
一般的除法只是取整数部分,达不到要求。
二、方法:
1、通用除法:
UP(A/B) = int((A+B-1)/B)
取临界值,计算下A+B-1的范围就OK.
2 、Python除法:
首先要说的是python中的除法运算,在python 2.5版本中存在两种除法运算,即所谓的true除法和floor除法。
当使用x/y形式进行除法运算时,如果x和y都是整形,那么运算的会对结果进行截取,取运算的整数部分,比如2/3的运算结果是0;如果x和y中有一个是浮点数,那么会进行所谓的true除法,比如2.0/3的结果是 0.66666666666666663。
另外一种除法是采用x//y的形式(向下取整),那么这里采用的是所谓floor除法,即得到不大于结果的最大整数值,这个运算时与操作数无关的。比如2//3的结果是0,-2//3的结果是-1,-2.0//3的结果是-1.0。
在python 3.0中,x/y将只执行true除法,而与操作数无关;x//y则执行floor除法。
如果需要在2.5版本的python中进行这样的用法,则需要在代码前加入from __future__ import division的声明。如:
from __future__ import division
a=2/3
这时变量a的结果将是0.66666666666666663,而不是原来的0了。
Python运算向上取整方法:(A+B-1)/B
3、Python match.ceil函数
ceil(x)函数是向上取整,即取大于等于x的最接近整数。
import math
math.ceil(float(205)/20)
另外:向下取整,一般使用floor除法,一般除法/,round()四舍五入函数。
Python 向上取整的算法的更多相关文章
- python 向上取整ceil 向下取整floor 四舍五入round
#encoding:utf-8 import math #向上取整 http://www.manongjc.com/article/1335.html print "math.ceil--- ...
- Python向上取整,向下取整以及四舍五入函数
import math f = 11.2 print math.ceil(f) #向上取整 print math.floor(f) #向下取整 print round(f) #四舍五入 #这三个函数的 ...
- python向上取整 向下取整
向上取整 ceil() 函数返回数字的向上取整整数,就是返回大于等于变量的最近的整数. ceil()是不能直接访问的,需要导入 math 模块. import math math.ceil( x ) ...
- python(50):python 向上取整 ceil 向下取整 floor 四舍五入 round
取整:ceil 向下取整:floor 四舍五入:round 使用如下:
- python向上取整以50为界
import math def getNum(limit_num,num): if num%limit_num==0: print(num) else: num=math.ceil(num/limit ...
- python 精确计算与向上取整 decimal math.ceil
1. 精确计算 python的float型不精确,需要导入decimal包,以下是不精确举例: 导入decimal包后: 2. 向上取整 一般的取整数(向下取整): 向上取整的方法:
- python 向下取整,向上取整,四舍五入
# python 向下取整 floor 向上取整ceil 四舍五入 round import math num=3.1415926 # 向上取整 print(math.ceil(num)) # 向下取 ...
- python中的向上取整向下取整以及四舍五入的方法
import math #向上取整print "math.ceil---"print "math.ceil(2.3) => ", math.ceil(2. ...
- Python 之 向上取整、向下取整以及四舍五入函数
import math f = 11.2 print math.ceil(f) #向上取整 print math.floor(f) #向下取整 print round(f) #四舍五入 #这三个函数的 ...
随机推荐
- timus 1106 Two Teams(二部图)
Two Teams Time limit: 1.0 secondMemory limit: 64 MB The group of people consists of N members. Every ...
- java多线程:java队列详解
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在队列这 ...
- Android Studio 使用教程
http://www.tuicool.com/articles/amMvM3B 用 Android Studio 开发安卓 APP-使用篇 http://ask.android-studio.org/ ...
- 运用@media实现网页自适应中的几个关键分辨率
http://jingyan.baidu.com/article/6f2f55a1ab36c3b5b83e6c46.html http://www.5imoban.net/jiaocheng/div+ ...
- sed详解
1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后 ...
- OCR是用来做什么的
OCR(Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译成 ...
- 自定义Mvc5 Owin 验证
public class AuthIn : IUserAuthenticate { public static ApplicationUserManager UserManager { get { r ...
- Linux-LVS+keepalived-Testing
LVS:Linux Virtual Server+++++++++++++Info+++++++++++VIP:172.18.20.222LVS-Master IP:172.18.20.206LVS- ...
- 【转】.NET多种WebKit内核/Blink内核浏览器初步测评报告
第1篇:.NET多种WebKit内核/Blink内核浏览器初步测评报告 本文转自“吾乐吧软件站”,原文链接:http://www.wuleba.com/?p=23590 报告研究时间:2013-10- ...
- java中的循环
while循环: while(循环条件){ 循环操作 } 停止while循环有两种方式:1.不再满足while后的循环条件时,循环终止: ...