python中round函數(shù)如何使用
round函數(shù)很簡單,對(duì)浮點(diǎn)數(shù)進(jìn)行近似取值,保留幾位小數(shù)。比如
>>> round(10.0/3, 2) 3.33 >>> round(20/7) 3
第一個(gè)參數(shù)是一個(gè)浮點(diǎn)數(shù),第二個(gè)參數(shù)是保留的小數(shù)位數(shù),可選,如果不寫的話默認(rèn)保留到整數(shù)。
這么簡單的函數(shù),能有什么坑呢?
1、round的結(jié)果跟python版本有關(guān)
我們來看看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", "copyright", "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.” 保留值將保留到離上一位更近的一端(四舍六入),如果距離兩端一樣遠(yuǎn),則保留到離0遠(yuǎn)的一邊。所以round(0.5)會(huì)近似到1,而round(-0.5)會(huì)近似到-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.” 如果距離兩邊一樣遠(yuǎn),會(huì)保留到偶數(shù)的一邊。比如round(0.5)和round(-0.5)都會(huì)保留到0,而round(1.5)會(huì)保留到2。
所以如果有項(xiàng)目是從py2遷移到py3的,可要注意一下round的地方(當(dāng)然,還要注意/和//,還有print,還有一些比較另類的庫)。
2、特殊數(shù)字round出來的結(jié)果可能未必是想要的。
>>> round(2.675, 2) 2.67
python2和python3的doc中都舉了個(gè)相同的栗子,原文是這么說的:
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) 的結(jié)果,不論我們從python2還是3來看,結(jié)果都應(yīng)該是2.68的,結(jié)果它偏偏是2.67,為什么?這跟浮點(diǎn)數(shù)的精度有關(guān)。我們知道在機(jī)器中浮點(diǎn)數(shù)不一定能精確表達(dá),因?yàn)閾Q算成一串1和0后可能是無限位數(shù)的,機(jī)器已經(jīng)做出了截?cái)嗵幚?。那么在機(jī)器中保存的2.675這個(gè)數(shù)字就比實(shí)際數(shù)字要小那么一點(diǎn)點(diǎn)。這一點(diǎn)點(diǎn)就導(dǎo)致了它離2.67要更近一點(diǎn)點(diǎn),所以保留兩位小數(shù)時(shí)就近似到了2.67。
以上。除非對(duì)精確度沒什么要求,否則盡量避開用round()函數(shù)。近似計(jì)算我們還有其他的選擇:
使用math模塊中的一些函數(shù),比如math.ceiling(天花板除法)。
python自帶整除,python2中是/,3中是//,還有div函數(shù)。
字符串格式化可以做截?cái)嗍褂?,例?"%.2f" % value(保留兩位小數(shù)并變成字符串……如果還想用浮點(diǎn)數(shù)請(qǐng)披上float()的外衣)。
當(dāng)然,對(duì)浮點(diǎn)數(shù)精度要求如果很高的話,請(qǐng)用嘚瑟饃,不對(duì)不對(duì),請(qǐng)用decimal模塊。
內(nèi)容擴(kuò)展:
round(number,num_digits)
Number 需要進(jìn)行四舍五入的數(shù)字。
Num_digits 指定的位數(shù),按此位數(shù)進(jìn)行四舍五入。
注解
- 如果 num_digits 大于 0,則四舍五入到指定的小數(shù)位。
- 如果 num_digits 等于 0,則四舍五入到最接近的整數(shù)。
- 如果 num_digits 小于 0,則在小數(shù)點(diǎn)左側(cè)進(jìn)行四舍五入。
示例
x=1.343671234 print x print round(x,1) print round(x,2) print round(x,3)
輸出結(jié)果為:
1.343671234
1.3
1.34
1.344
到此這篇關(guān)于python中round函數(shù)如何使用的文章就介紹到這了,更多相關(guān)python的round函數(shù)用法總結(jié)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中plt.imshow與cv2.imshow顯示顏色問題
這篇文章主要介紹了plt.imshow與cv2.imshow顯示顏色問題,本文給大家介紹的非常詳細(xì),同時(shí)給大家提到了cv2.imshow()和plt.imshow()的區(qū)別講解,需要的朋友可以參考下2020-07-07python?Matplotlib繪制炫酷柱狀圖的藝術(shù)與技巧大全
柱狀圖(Bar Plot)是一種常用的數(shù)據(jù)可視化方式,用于顯示各個(gè)類別之間的比較,下面這篇文章主要給大家介紹了關(guān)于python?Matplotlib繪制炫酷柱狀圖的藝術(shù)與技巧大全,需要的朋友可以參考下2024-03-03詳解Python中的from..import絕對(duì)導(dǎo)入語句
絕對(duì)導(dǎo)入其實(shí)非常簡單,即是用from語句在import前指明頂層package名,下面我們通過兩個(gè)例子來詳解Python中的from..import絕對(duì)導(dǎo)入語句2016-06-06python超詳細(xì)實(shí)現(xiàn)字體反爬流程
大家好,本篇文章主要講的是python查策網(wǎng)字體反爬實(shí)例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-05-05python 獲取計(jì)算機(jī)的網(wǎng)卡信息
這篇文章主要介紹了python 獲取計(jì)算機(jī)的網(wǎng)卡信息的方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2021-02-02