python-取整方法

參考:https://www.jianshu.com/p/75c00c91f6c5
參考:https://www.cnblogs.com/mainstream/p/11383136.html

Python取整的一些方法

  1. 向下取整:int() , math.floor()
>>> a = 12.66
>>> int(a)
12
>>> math.floor(3.14)
3
>>> math.floor(3.54)
3
  1. 向上取整:math.ceil()
>>> import math
>>> math.ceil(3.14)
4
>>> math.ceil(3.66)
4
  1. 四捨五入:round()
>>> round(3.14)
3
>>> round(3.54)
4
  1. 分別取整數部分和小數部分
>>> math.modf(3.52)
(0.52, 3.0)
>>> math.modf(6.14)
(0.13999999999999968, 6.0)

最後一個結果應該是(0.14, 6.0),而存在的問題是:浮點數在計算機中的表示。當前技術支持下,浮點數在計算機中是無法精確的表示小數的。python採用IEEE 754規範來存儲浮點數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章