參考:https://www.jianshu.com/p/75c00c91f6c5
參考:https://www.cnblogs.com/mainstream/p/11383136.html
Python取整的一些方法
- 向下取整:int() , math.floor()
>>> a = 12.66
>>> int(a)
12
>>> math.floor(3.14)
3
>>> math.floor(3.54)
3
- 向上取整:math.ceil()
>>> import math
>>> math.ceil(3.14)
4
>>> math.ceil(3.66)
4
- 四捨五入:round()
>>> round(3.14)
3
>>> round(3.54)
4
- 分別取整數部分和小數部分
>>> math.modf(3.52)
(0.52, 3.0)
>>> math.modf(6.14)
(0.13999999999999968, 6.0)
最後一個結果應該是(0.14, 6.0)
,而存在的問題是:浮點數在計算機中的表示。當前技術支持下,浮點數在計算機中是無法精確的表示小數的。python採用IEEE 754
規範來存儲浮點數。