python 取整_马克的Python学习笔记#数字,日期和时间
對數(shù)值進(jìn)行調(diào)整
在Python中對整數(shù)和浮點(diǎn)數(shù)進(jìn)行數(shù)字計算是很容易的。但是,如果你需要對分?jǐn)?shù),數(shù)組或者日期和時間進(jìn)行計算,這就會稍微復(fù)雜點(diǎn)。對于簡單的取整操作,我們可以使用內(nèi)建的round(value, ndigits)函數(shù)就可,舉個例子:
>>> round(1.23, 1) 1.2 >>> round(-1.23, 1) -1.2 >>> round(1.39, 1) 1.4 >>> round(3.1415926535, 3) 3.142 >>>當(dāng)某個值恰好等于兩個整數(shù)間的一半的時候,取整操作會取到離這個值最近的那個偶數(shù)上,并且,傳遞給round()的參數(shù)ndigits可以是負(fù)數(shù),在這種情況下會相應(yīng)的取整鄰近位數(shù)上,舉個例子:
>>> a = 31415926535897932626 >>> round(a, -1) 31415926535897932630 >>> round(a, -1) 31415926535897932630 >>> round(a,-2) 31415926535897932600 >>> round(a, -9) 31415926536000000000 >>>但是注意,在這種情況下只會輸出0.0
>>> a = -3.1415926535897932626 >>> round (a, -1) -0.0因?yàn)楹竺婺莻€-1實(shí)際上就是取的-3四舍五入就等于0了
注意:在對值進(jìn)行輸出的時候不要把取整和格式化的操作混為一談。如果將數(shù)值以固定的位數(shù)輸出,一般情況下是用不上round()的,相反,我們只要在格式化時指定所需要的精度就可以了。此外,不要采用對浮點(diǎn)數(shù)進(jìn)行取整的方式來進(jìn)行修正精度上的問題。對于大部分的涉及浮點(diǎn)數(shù)的應(yīng)用程序來說,一般來講都沒必要,但是如果遇到避免出現(xiàn)誤差的行為非常重要,那么就可以考慮使用decimal模塊
至于四舍五入的運(yùn)算在IDE上怎么寫,可以參考下這個:
def round_num():print(round(1.23, 1))print(round(1.27, 1))print(round(-1.27, 1))print(round(1.25361,3))# 舍入數(shù)為負(fù)數(shù)a = 1627731print(round(a, -1))print(round(a, -2))print(round(a, -3))# 格式化輸出x = 1.23456print(format(x, '0.2f'))print(format(x, '0.3f'))print('value is {:0.3f}'.format(x))# 不要自以為是的用round去修正一些精度問題a = 2.1b = 4.2c = a + bprint(c)c = round(c, 2) # "Fix" result (???)print(c)if __name__ == '__main__':round_num()執(zhí)行精確的小數(shù)計算
關(guān)于浮點(diǎn)數(shù),有一個所有人都直到的問題就是它們無法精確表達(dá)所有的十進(jìn)制小數(shù)位,并且甚至連簡單的數(shù)學(xué)計算也會引入微小的誤差。這些誤差實(shí)際上是由底層CPU的浮點(diǎn)運(yùn)算單元和IEEE 754浮點(diǎn)算術(shù)標(biāo)準(zhǔn)的一種特性,由于Python的浮點(diǎn)類型保存的數(shù)據(jù)采用的是原始表示形式,因此這種誤差是無法避免的,除非你不用float實(shí)例。我們目前能做的就是通過decimal模塊來加強(qiáng)精度,但這樣會犧牲掉一些性能:
>>> from decimal import Decimal >>> a = Decimal('1.15') >>> b = Decimal('1.17') >>> a + b Decimal('2.32') >>> print(a + b) 2.32 >>> (a + b) == Decimal('2.32') True >>>Decimal對象能夠以任何你所期待的方式來工作,這個模塊的主要功能就是允許控制計算過程中的各個方面,這包括數(shù)字的位數(shù)和四舍五入。
代碼示例:
from decimal import Decimal from decimal import localcontext import mathdef acc_deciamal():a = 4.2b = 2.1print(a + b)print((a + b) == 6.3)# 使用decimal模塊a = Decimal('4.2')b = Decimal('2.1')print(a + b)print((a + b) == Decimal('6.3'))a = Decimal('1.3')b = Decimal('1.7')print(a / b)with localcontext() as ctx:ctx.prec = 3print(a / b)nums = [1.23e+18, 1, -1.23e+18]print(sum(nums))print(math.fsum(nums))if __name__ == '__main__':acc_deciamal()對數(shù)值進(jìn)行格式化輸出
如果我們需要對一個單獨(dú)的數(shù)值做格式化的輸出,就像我前幾章講的那樣,直接使用內(nèi)奸函數(shù)format()輸出就可:
def format_number():x = 1234.56789# Two decimal places of accuracyprint(format(x, '0.2f'))# Right justified in 10 chars, one-digit accuracyprint(format(x, '>10.1f'))# Left justifiedprint(format(x, '<10.1f'))# Centeredprint(format(x, '^10.1f'))# Inclusion of thousands separatorprint(format(x, ','))print(format(x, '0,.1f'))print(format(x, 'e'))print(format(x, '0.2E'))# stringsprint('The value is {:0,.2f}'.format(x))print(format(x, '0.1f'))print(format(-x, '0.1f'))swap_separators = {ord('.'): ',', ord(','): '.'}print(format(x, ',').translate(swap_separators))if __name__ == '__main__':format_number()對數(shù)值做格式化輸出通常都是很直接的,上面的例子既可以用于浮點(diǎn)型整數(shù),也可以用于decimal模塊中的Decimal對象。當(dāng)需要限制數(shù)值是位數(shù)的時候,數(shù)值會根據(jù)round()函數(shù)的規(guī)則來進(jìn)行取整。注意:對數(shù)值加上千位分隔符的格式化操作并不是特定于本地的環(huán)境的,當(dāng)然,如果你需要,應(yīng)該可以用local模塊的函數(shù)以及用字符串的translate()方法來交換分隔符。
參考書目:
《Python CookBook》作者:【美】 David Beazley, Brian K. Jones
Github地址:
yidao620c/python3-cookbook?github.com總結(jié)
以上是生活随笔為你收集整理的python 取整_马克的Python学习笔记#数字,日期和时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件开发报价模板_定制开发小程序和行业通
- 下一篇: socket timeout是什么引起的