0
点赞
收藏
分享

微信扫一扫

在鲲鹏服务器上安装nginx

践行数据分析 2024-05-31 阅读 9
笔记python

取整方式包括向下取整、四舍五入取整、向上取整等。

1、向下取整:向下取整很简单,就是去掉小数部分,直接使用 int() 函数即可。

print(int(1.9)) # 运行结果:1
print(int(1.1))	# 运行结果:1
print(int(1.5))	# 运行结果:1

2、四舍五入取整:四舍五入取整用到的是round()函数。

格式:round(x,n)

作用是返回x四舍五入后的值,其中n表示保留小数的位数。

print(round(1.99999,2))	# 运行结果:2.0
print(round(1.19999,1))	# 运行结果:1.2
print(round(1.511111,3))	# 运行结果:1.511

round(x) 则表示四舍五入取整数。

print(round(1.9))	# 运行结果:2
print(round(1.1))	# 运行结果:1
print(round(1.5))	# 运行结果:2

3、向上取整:Python的math库中带有向上取整的函数,即 ceil() 函数。

import math
print(math.ceil(1.9))	# 运行结果:2
print(math.ceil(1.1))	# 运行结果:2
print(math.ceil(1.5))	# 运行结果:2
举报

相关推荐

0 条评论