一、初衷:

  有时候我们分页展示数据的时候,需要计算页数。一般都是向上取整,例如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 向上取整的算法的更多相关文章

  1. python 向上取整ceil 向下取整floor 四舍五入round

    #encoding:utf-8 import math #向上取整 http://www.manongjc.com/article/1335.html print "math.ceil--- ...

  2. Python向上取整,向下取整以及四舍五入函数

    import math f = 11.2 print math.ceil(f) #向上取整 print math.floor(f) #向下取整 print round(f) #四舍五入 #这三个函数的 ...

  3. python向上取整 向下取整

    向上取整 ceil() 函数返回数字的向上取整整数,就是返回大于等于变量的最近的整数. ceil()是不能直接访问的,需要导入 math 模块. import math math.ceil( x ) ...

  4. python(50):python 向上取整 ceil 向下取整 floor 四舍五入 round

    取整:ceil 向下取整:floor 四舍五入:round 使用如下:

  5. python向上取整以50为界

    import math def getNum(limit_num,num): if num%limit_num==0: print(num) else: num=math.ceil(num/limit ...

  6. python 精确计算与向上取整 decimal math.ceil

    1. 精确计算 python的float型不精确,需要导入decimal包,以下是不精确举例: 导入decimal包后: 2. 向上取整 一般的取整数(向下取整): 向上取整的方法:

  7. python 向下取整,向上取整,四舍五入

    # python 向下取整 floor 向上取整ceil 四舍五入 round import math num=3.1415926 # 向上取整 print(math.ceil(num)) # 向下取 ...

  8. python中的向上取整向下取整以及四舍五入的方法

    import math #向上取整print "math.ceil---"print "math.ceil(2.3) => ", math.ceil(2. ...

  9. Python 之 向上取整、向下取整以及四舍五入函数

    import math f = 11.2 print math.ceil(f) #向上取整 print math.floor(f) #向下取整 print round(f) #四舍五入 #这三个函数的 ...

随机推荐

  1. URAL 1069 Prufer Code(模拟)

    Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without cycles) ...

  2. 【NOIP2012】国王游戏

    这一次高精度完美地过辣好开心OvO,还get到了非常方便的高精度除小于10000的方法,这个是我自己脑出来的OvO 看来下午高精度傻逼得值qvq 原题: 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个 ...

  3. Git入门指南十一:Git branch 分支与合并分支

    十五. Git branch 分支 查看当前有哪些branch bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch * master ...

  4. ogistic regression (逻辑回归) 概述

    :http://hi.baidu.com/hehehehello/blog/item/0b59cd803bf15ece9023d96e.html#send http://en.wikipedia.or ...

  5. 引用POPUI来实现弹窗效果,且弹窗中的内容可以点击事件

    seajs.use(['../js/ui/dialog'],function(){ $('.center-button').bind('click',function(){ var $dlg = $. ...

  6. The repository for high quality TypeScript type definitions

    Best practices This is a guide to the best practices to follow when creating typing files. There are ...

  7. python sys.argv[]

    sys.argv[]是用来获取命令行参数的,是一个由该脚本自身路径和其它输入的参数组成的List.sys.argv[0]表示代码本身文件路径. 这里,当我们执行python using_sys.py ...

  8. [jquery] jQuery jsTree V3.2.1 基础Demo

    引入对应的文件: <link rel="stylesheet" href="../dist/themes/default/style.min.css" / ...

  9. 项目ppt演讲与阶段性总结

    ☆车老师讲解PPT项目: 1.汉企0410天启网络公司 2.Ppt--画龙点睛 3.项目制作背景-->点到人心上,别一堆文字,别虚,点出1234 4.说话量化.具象化:明天下午5.00做完,做不 ...

  10. synergy帮组提升办公效率

    这个synergy确实很不错哦,当你在办公室拥有两台或者多台电脑的时候,放在面前多台显示器,多个鼠标,多个键盘,但是你的桌面上,是不是多出了些你不需要看到的键盘或者鼠标?至少我是这样子的,我希望多个显 ...