大家好,又见面了,我是你们的朋友全栈君。
一、初衷:
有时候我们分页展示数据的时候,需要计算页数。一般都是向上取整,例如counts=205 pageCouts=20 ,pages= 11 页。
一般的除法只是取整数部分,达不到要求。
二、方法:
1、通用除法:
UP(A/B) = int((A+B-1)/B)
取临界值,计算下A+B-1的范围就OK.
2 、Python除法:
首先要说的是python中的除法运算,
在python 3.0中,x/y将只执行true除法,而与操作数无关;x//y则执行floor除法。
Python运算向上取整方法:(A+B-1)/B
3、Python match.ceil函数 np.ceil函数
ceil(x)函数是向上取整,即取大于等于x的最接近整数。
import math
math.ceil(float(205)/20)
import numpy as np
np.ceil(float(205)/20)
1、一般使用floor除法 (np.floor()或者math.floor())
import numpy as np
n = np.array([-1.7, -2.5, -0.2, 0.6, 1.2, 2.7, 11])
floor = np.floor(n)
print(floor) # [ -2. -3. -1. 0. 1. 2. 11.]
2、一般除法/
A=100
B=16
c=100//16
(c=6)
3、round()四舍五入函数。
np.around 返回四舍五入后的值,可指定精度。
around(a, decimals=0, out=None)
a 输入数组 decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置
import numpy as np
n = np.array([-0.746, 4.6, 9.4, 7.447, 10.455, 11.555])
around1 = np.around(n)
print(around1) # [ -1. 5. 9. 7. 10. 12.]
around2 = np.around(n, decimals=1)
print(around2) # [ -0.7 4.6 9.4 7.4 10.5 11.6]
around3 = np.around(n, decimals=-1)
print(around3) # [ -0. 0. 10. 10. 10. 10.]
math模块中的 modf()方法
将整数部分和小数部分分别取出,可以使用math模块中的 modf()方法
例如:
>>> math.modf(4.25)
(0.25, 4.0)
>>> math.modf(4.33)
(0.33000000000000007, 4.0)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/126994.html原文链接:https://javaforall.cn