這篇文章主要介紹了python不保留小數的方法有哪些,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
1、int() 向下取整(內置函數)
n = 3.75 print(int(n)) >>> 3 n = 3.25 print(int(n)) >>> 3
2、round() 四舍五入(內置函數)
n = 3.75 print(round(n)) >>> 4 n = 3.25 print(round(n)) >>> 3
3、floor() 向下取整(math模塊函數)
floor的英文釋義:地板。顧名思義也是向下取整
import math n = 3.75 print(math.floor(n)) >>> 3 n = 3.25 print(math.floor(n)) >>> 3
4、ceil()向上取整 (math模塊函數)
ceil的英文釋義:天花板。
import math n = 3.75 print(math.ceil(n)) >>> 4 n = 3.25 print(math.ceil(n)) >>> 4
感謝你能夠認真閱讀完這篇文章,希望小編分享python不保留小數的方法有哪些內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。