首页 > 文章列表 > python如何不保留小数

python如何不保留小数

Python 小数 不保留
421 2022-08-07

1、int() 向下取整(内置函数) 

n = 3.75
print(int(n))
>>> 3
n = 3.25
print(int(n))
>>> 3

2、round() 四舍五入(内置函数) 

n = 3.75
print(round(n))
>>> 4
n = 3.25
print(round(n))
>>> 3

3、floor() 向下取整(math模块函数) 

floor的英文释义:地板。顾名思义也是向下取整

import math
n = 3.75
print(math.floor(n))
>>> 3
n = 3.25
print(math.floor(n))
>>> 3

4、ceil()向上取整 (math模块函数)

ceil的英文释义:天花板。

import math
n = 3.75
print(math.ceil(n))
>>> 4
n = 3.25
print(math.ceil(n))
>>> 4