1 In [1]: a = 5.026
2 3 In [2]: b = 5.000 4 5 In [3]: round(a,2) 6 Out[3]: 5.03 7 8 In [4]: round(b,2) 9 Out[4]: 5.0 10 11 In [5]: '%.2f' % a 12 Out[5]: '5.03' 13 14 In [6]: '%.2f' % b 15 Out[6]: '5.00' 16 17 In [7]: float('%.2f' % a) 18 Out[7]: 5.03 19 20 In [8]: float('%.2f' % b) 21 Out[8]: 5.0 22 23 In [9]: from decimal import Decimal 24 25 In [10]: Decimal('5.026').quantize(Decimal('0.00')) 26 Out[10]: Decimal('5.03') 27 28 In [11]: Decimal('5.000').quantize(Decimal('0.00')) 29 Out[11]: Decimal('5.00')
這里有三種方法,
round(a,2)
'%.2f' % a
Decimal('5.000').quantize(Decimal('0.00'))
當需要輸出的結果要求有兩位小數的時候,字符串形式的:'%.2f' % a 方式最好,其次用Decimal。
需要注意的:
1. 可以傳遞給Decimal整型或者字符串參數,但不能是浮點數據,因為浮點數據本身就不准確。
2. Decimal還可以用來限定數據的總位數。
談談關於Python里面小數點精度控制的問題
基礎
浮點數是用機器上浮點數的本機雙精度(64 bit)表示的。提供大約17位的精度和范圍從-308到308的指數。和C語言里面的double類型相同。Python不支持32bit的單精度浮點數。如果程序需要精確控制區間和數字精度,可以考慮使用numpy擴展庫。
Python 3.X對於浮點數默認的是提供17位數字的精度。
關於單精度和雙精度的通俗解釋:
單精度型和雙精度型,其類型說明符為float 單精度說明符,double 雙精度說明符。在Turbo C中單精度型占4個字節(32位)內存空間,其數值范圍為3.4E-38~3.4E+38,只能提供七位有效數字。雙精度型占8 個字節(64位)內存空間,其數值范圍為1.7E-308~1.7E+308,可提供16位有效數字。
要求較小的精度
將精度高的浮點數轉換成精度低的浮點數。
1.round()內置方法
這個是使用最多的,剛看了round()的使用解釋,也不是很容易懂。round()不是簡單的四舍五入的處理方式。
For the built-in types supporting round(), 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 (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2).
1 >>> round(2.5) 2 2 3 >>> round(1.5) 4 2 5 >>> round(2.675) 6 3 7 >>> round(2.675, 2) 8 2.67
round()如果只有一個數作為參數,不指定位數的時候,返回的是一個整數,而且是最靠近的整數(這點上類似四舍五入)。但是當出現.5的時候,兩邊的距離都一樣,round()取靠近的偶數,這就是為什么round(2.5) = 2。當指定取舍的小數點位數的時候,一般情況也是使用四舍五入的規則,但是碰到.5的這樣情況,如果要取舍的位數前的小數是奇數,則向下取舍,如果偶數則向上取舍。看下面的示例:
1 >>> round(2.635, 2) 2 2.63 3 >>> round(2.645, 2) 4 2.65 5 >>> round(2.655, 2) 6 2.65 7 >>> round(2.665, 2) 8 2.67 9 >>> round(2.675, 2) 10 2.67
2. 使用格式化
效果和round()是一樣的。
1 >>> a = ("%.2f" % 2.635) 2 >>> a 3 '2.63' 4 >>> a = ("%.2f" % 2.645) 5 >>> a 6 '2.65' 7 >>> a = int(2.5) 8 >>> a 9 2
要求超過17位的精度分析
python默認的是17位小數的精度,但是這里有一個問題,就是當我們的計算需要使用更高的精度(超過17位小數)的時候該怎么做呢?
1. 使用格式化(不推薦)
1 >>> a = "%.30f" % (1/3) 2 >>> a 3 '0.333333333333333314829616256247'
可以顯示,但是不准確,后面的數字往往沒有意義。
2. 高精度使用decimal模塊,配合getcontext
1 >>> from decimal import * 2 >>> print(getcontext()) 3 Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow]) 4 >>> getcontext().prec = 50 5 >>> b = Decimal(1)/Decimal(3) 6 >>> b 7 Decimal('0.33333333333333333333333333333333333333333333333333') 8 >>> c = Decimal(1)/Decimal(17) 9 >>> c 10 Decimal('0.058823529411764705882352941176470588235294117647059') 11 >>> float(c) 12 0.058823529411764705
默認的context的精度是28位,可以設置為50位甚至更高,都可以。這樣在分析復雜的浮點數的時候,可以有更高的自己可以控制的精度。其實可以留意下context里面的這rounding=ROUND_HALF_EVEN 參數。ROUND_HALF_EVEN, 當half的時候,靠近even.
關於小數和取整
既然說到小數,就必然要說到整數。一般取整會用到這些函數:
1. round()
這個不說了,前面已經講過了。一定要注意它不是簡單的四舍五入,而是ROUND_HALF_EVEN的策略。
2. math模塊的ceil(x)
取大於或者等於x的最小整數。
3. math模塊的floor(x)
去小於或者等於x的最大整數。
1 >>> from math import ceil, floor 2 >>> round(2.5) 3 2 4 >>> ceil(2.5) 5 3 6 >>> floor(2.5) 7 2 8 >>> round(2.3) 9 2 10 >>> ceil(2.3) 11 3 12 >>> floor(2.3) 13 2 14 >>>