python中round函式如何使用
round函式很簡單,對浮點數進行近似取值,保留幾位小數。比如
>>> round(10.0/3,2) 3.33 >>> round(20/7) 3
第一個引數是一個浮點數,第二個引數是保留的小數位數,可選,如果不寫的話預設保留到整數。
這麼簡單的函式,能有什麼坑呢?
1、round的結果跟python版本有關
我們來看看python2和python3中有什麼不同:
$ python Python 2.7.8 (default,Jun 18 2015,18:54:19) [GCC 4.9.1] on linux2 Type "help","copyright","credits" or "license" for more information. >>> round(0.5) 1.0
$ python3 Python 3.4.3 (default,Oct 14 2015,20:28:29) [GCC 4.8.4] on linux Type "help","credits" or "license" for more information. >>> round(0.5)
如果我們閱讀一下python的文件,裡面是這麼寫的:
在python2.7的doc中,round()的最後寫著,“Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close,rounding is done away from 0.” 保留值將保留到離上一位更近的一端(四捨六入),如果距離兩端一樣遠,則保留到離0遠的一邊。所以round(0.5)會近似到1,而round(-0.5)會近似到-1。
但是到了python3.5的doc中,文件變成了“values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close,rounding is done toward the even choice.” 如果距離兩邊一樣遠,會保留到偶數的一邊。比如round(0.5)和round(-0.5)都會保留到0,而round(1.5)會保留到2。
所以如果有專案是從py2遷移到py3的,可要注意一下round的地方(當然,還要注意/和//,還有print,還有一些比較另類的庫)。
2、特殊數字round出來的結果可能未必是想要的。
>>> round(2.675,2) 2.67
python2和python3的doc中都舉了個相同的栗子,原文是這麼說的:
Note
The behavior of round() for floats can be surprising: for example,round(2.675,2) gives 2.67 instead of the expected
2.68. This is not a bug: it's a result of the fact that most decimal fractions can't be represented exactly as a
float. See Floating Point Arithmetic: Issues and Limitations for more information.
簡單的說就是,round(2.675,2) 的結果,不論我們從python2還是3來看,結果都應該是2.68的,結果它偏偏是2.67,為什麼?這跟浮點數的精度有關。我們知道在機器中浮點數不一定能精確表達,因為換算成一串1和0後可能是無限位數的,機器已經做出了截斷處理。那麼在機器中儲存的2.675這個數字就比實際數字要小那麼一點點。這一點點就導致了它離2.67要更近一點點,所以保留兩位小數時就近似到了2.67。
以上。除非對精確度沒什麼要求,否則儘量避開用round()函式。近似計算我們還有其他的選擇:
使用math模組中的一些函式,比如math.ceiling(天花板除法)。
python自帶整除,python2中是/,3中是//,還有div函式。
字串格式化可以做截斷使用,例如 "%.2f" % value(保留兩位小數並變成字串……如果還想用浮點數請披上float()的外衣)。
當然,對浮點數精度要求如果很高的話,請用嘚瑟饃,不對不對,請用decimal模組。
內容擴充套件:
round(number,num_digits)
Number 需要進行四捨五入的數字。
Num_digits 指定的位數,按此位數進行四捨五入。
註解
- 如果 num_digits 大於 0,則四捨五入到指定的小數位。
- 如果 num_digits 等於 0,則四捨五入到最接近的整數。
- 如果 num_digits 小於 0,則在小數點左側進行四捨五入。
示例
x=1.343671234 print x print round(x,1) print round(x,2) print round(x,3)
輸出結果為:
1.343671234
1.3
1.34
1.344
到此這篇關於python中round函式如何使用的文章就介紹到這了,更多相關python的round函式用法總結內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!