Python教程

python对数字的四种取整方法int、math.ceil、round、math.modf

本文主要是介绍python对数字的四种取整方法int、math.ceil、round、math.modf,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

      • 1. int(): 向下取整; 3.7取3;
      • 2. math.ceil(): 向上取整; 3.2取4;
      • 3. round(): 四舍五入;
      • 4. math.modf(): 取整数部分和小数部分,返回一个元组:(小数部分,整数部分)。注意小数部分的结果有异议

1. int(): 向下取整; 3.7取3;

2. math.ceil(): 向上取整; 3.2取4;

3. round(): 四舍五入;

4. math.modf(): 取整数部分和小数部分,返回一个元组:(小数部分,整数部分)。注意小数部分的结果有异议

import math
flo1 = 3.1415
flo2 = 3.500
flo3 = 3.789
print(int(flo1),math.ceil(flo1),round(flo1),math.modf(flo1))
print(int(flo2),math.ceil(flo2),round(flo2),math.modf(flo2))
print(int(flo3),math.ceil(flo3),round(flo3),math.modf(flo3))
"""
int   ceil  round      modf
 3     4      3    (0.14150000000000018, 3.0)
 3     4      4    (0.5, 3.0)
 3     4      4    (0.7890000000000001, 3.0)
"""

参考:https://www.cnblogs.com/huangqihui/p/12125109.html

这篇关于python对数字的四种取整方法int、math.ceil、round、math.modf的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!