python对数字的四种取整方法int、math.ceil、round、math.modf
2021/12/6 1:18:52
本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-27使用python 将ETH账户的资产打散
- 2024-09-26Python编程基础
- 2024-09-2610 种方法写出更好的 Python 代码
- 2024-09-25Python编程基础详解
- 2024-09-25Python编程入门教程
- 2024-09-25从零开始使用Python构建LLaMA 3
- 2024-09-23Python中理解和使用树形结构的简单教程
- 2024-09-23Python 编程基础入门
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战