python3中為什么 round(0.5) == 0

問(wèn)題如標(biāo)題

python3官方文檔也給出過(guò)解釋

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.

▲▲附贈(zèng)

使用python3實(shí)現(xiàn)真四舍五入round()函數(shù)代碼

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

def trueround(number, places=0):
    '''
    trueround(number, places)

    example:

        >>> trueround(2.55, 1) == 2.6
        True

    uses standard functions with no import to give "normal" behavior to 
    rounding so that trueround(2.5) == 3, trueround(3.5) == 4, 
    trueround(4.5) == 5, etc. Use with caution, however. This still has 
    the same problem with floating point math. The return object will 
    be type int if places=0 or a float if places=>1.

    number is the floating point number needed rounding

    places is the number of decimal places to round to with '0' as the
        default which will actually return our interger. Otherwise, a
        floating point will be returned to the given decimal place.

    Note:   Use trueround_precision() if true precision with
            floats is needed

    GPL 2.0
    copywrite by Narnie Harshoe <signupnarnie@gmail.com>
    '''
    place = 10**(places)
    rounded = (int(number*place + 0.5if number>=0 else -0.5))/place
    if rounded == int(rounded):
        rounded = int(rounded)
    return rounded

def trueround_precision(number, places=0, rounding=None):
    '''
    trueround_precision(number, places, rounding=ROUND_HALF_UP)

    Uses true precision for floating numbers using the 'decimal' module in
    python and assumes the module has already been imported before calling
    this function. The return object is of type Decimal.

    All rounding options are available from the decimal module including 
    ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, 
    ROUND_HALF_UP, ROUND_UP, and ROUND_05UP.

    examples:

        >>> trueround(2.5, 0) == Decimal('3')
        True
        >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2')
        True

    number is a floating point number or a string type containing a number on 
        on which to be acted.

    places is the number of decimal places to round to with '0' as the default.

    Note:   if type float is passed as the first argument to the function, it
            will first be converted to a str type for correct rounding.

    GPL 2.0
    copywrite by Narnie Harshoe <signupnarnie@gmail.com>
    '''
    from decimal import Decimal as dec
    from decimal import ROUND_HALF_UP
    from decimal import ROUND_CEILING
    from decimal import ROUND_DOWN
    from decimal import ROUND_FLOOR
    from decimal import ROUND_HALF_DOWN
    from decimal import ROUND_HALF_EVEN
    from decimal import ROUND_UP
    from decimal import ROUND_05UP

    if type(number) == type(float()):
        number = str(number)
    if rounding == None:
        rounding = ROUND_HALF_UP
    place = '1.'
    for i in range(places):
        place = ''.join([place, '0'])
    return dec(number).quantize(dec(place), rounding=rounding)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市掖肋,隨后出現(xiàn)的幾起案子胳徽,更是在濱河造成了極大的恐慌,老刑警劉巖募寨,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赡茸,死亡現(xiàn)場(chǎng)離奇詭異蕊连,居然都是意外死亡悬垃,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)甘苍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)尝蠕,“玉大人,你說(shuō)我怎么就攤上這事载庭】幢耍” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵囚聚,是天一觀的道長(zhǎng)靖榕。 經(jīng)常有香客問(wèn)我,道長(zhǎng)顽铸,這世上最難降的妖魔是什么茁计? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮谓松,結(jié)果婚禮上星压,老公的妹妹穿的比我還像新娘践剂。我一直安慰自己,他們只是感情好娜膘,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布逊脯。 她就那樣靜靜地躺著,像睡著了一般竣贪。 火紅的嫁衣襯著肌膚如雪军洼。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,696評(píng)論 1 312
  • 那天演怎,我揣著相機(jī)與錄音歉眷,去河邊找鬼。 笑死颤枪,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的淑际。 我是一名探鬼主播畏纲,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼春缕!你這毒婦竟也來(lái)了盗胀?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤锄贼,失蹤者是張志新(化名)和其女友劉穎票灰,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體宅荤,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡屑迂,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了冯键。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惹盼。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖惫确,靈堂內(nèi)的尸體忽然破棺而出手报,到底是詐尸還是另有隱情,我是刑警寧澤改化,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布掩蛤,位于F島的核電站,受9級(jí)特大地震影響陈肛,放射性物質(zhì)發(fā)生泄漏揍鸟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一句旱、第九天 我趴在偏房一處隱蔽的房頂上張望蜈亩。 院中可真熱鬧懦窘,春花似錦、人聲如沸稚配。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)道川。三九已至午衰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間冒萄,已是汗流浹背臊岸。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留尊流,地道東北人帅戒。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像崖技,于是被迫代替她去往敵國(guó)和親逻住。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 〇迎献、前言 本文共108張圖瞎访,流量黨請(qǐng)慎重! 歷時(shí)1個(gè)半月吁恍,我把自己學(xué)習(xí)Python基礎(chǔ)知識(shí)的框架詳細(xì)梳理了一遍扒秸。 ...
    Raxxie閱讀 18,968評(píng)論 17 410
  • 模塊與包[1] 模塊與包的關(guān)系:模塊可以屬于包 導(dǎo)入模塊(module)的方法:直接使用import后綴名為.py...
    HerdingCat閱讀 346評(píng)論 0 8
  • 在程序運(yùn)行過(guò)程中,總會(huì)遇到各種各樣的錯(cuò)誤冀瓦。 有的錯(cuò)誤是程序編寫(xiě)有問(wèn)題造成的伴奥,比如本來(lái)應(yīng)該輸出整數(shù)結(jié)果輸出了字符串,...
    jbb_43b0閱讀 858評(píng)論 0 0
  • 69 個(gè)內(nèi)置函數(shù)翼闽,還是挺多的渔伯,呵呵,小意思 官方文檔: 一個(gè)個(gè)來(lái)吧: abs(x) 返回一個(gè)數(shù)的絕對(duì)值肄程。參數(shù)可以是...
    天空藍(lán)雨閱讀 280評(píng)論 0 0
  • 夜鶯2517閱讀 127,728評(píng)論 1 9