关于 python 四舍五入的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
forreal
V2EX    Python

关于 python 四舍五入的问题

  •  
  •   forreal 2014-05-29 15:03:11 +08:00 7907 次点击
    这是一个创建于 4230 天前的主题,其中的信息可能已经有所发展或是发生改变。
    初学python

    import decimal
    a = decimal.Decimal("0.5")
    decimal.getcontext().rounding = decimal.ROUND_UP

    print(round(a))

    如何能让a四舍五入到1?
    用的是python3
    13 条回复    2017-07-18 16:07:05 +08:00
    imn1
        1
    imn1  
       2014-05-29 15:14:37 +08:00   1
    转贴网上收集
    google中有人这么解决的:

    >>> from decimal import Decimal
    >>> n = Decimal('1.555')
    >>> round(n, 2)
    Decimal('1.56')

    现在使用的方式是:
    可以使用str.format来格式化数字实现四舍五入

    from decimal import Decimal
    In [15]: '{:.2f}'.format(Decimal('2.675'))
    Out[15]: '2.68''





    def myround(par,l):
    temp = 1
    for i in range(l):
    temp*=10
    v = int((par+0.5/temp)*temp) / temp
    return v

    i = 1.25
    print(myround(i,1))
    i = 1.245
    print(myround(i,2))
    i = 1.21
    print(myround(i,1))
    i = 1.249
    print(myround(i,2))

    ----
    1.3
    1.25
    1.2
    1.25
    forreal
        2
    forreal  
    OP
       2014-05-29 15:27:52 +08:00
    @imn1
    也就是说没有现成的函数?
    myround那个不错
    riaqn
        3
    riaqn  
       2014-05-29 15:30:15 +08:00  
    imn1
        4
    imn1  
       2014-05-29 16:28:42 +08:00
    @forreal 这个是浮点数精度问题,例如0.5实际上是0.49xxxxxxxxxxxxxx,四舍五入当然就是用了“舍”,
    0.5只是字面精度,所以上面用了字串方式处理也是合理的
    forreal
        5
    forreal  
    OP
       2014-05-29 16:39:53 +08:00
    @imn1
    字符串换成2.665就不对了

    from decimal import Decimal
    a = '{:.2f}'.format(Decimal('2.665'))
    print(a)
    imn1
        6
    imn1  
       2014-05-29 17:09:23 +08:00
    还真是呢,之前都是看,保存笔记,没怎么验证,实际应用也很少用到四舍五入
    lynx
        7
    lynx  
       2014-05-29 18:47:00 +08:00
    In [1]: import math

    In [2]: math.ceil(0.5)
    Out[2]: 1
    forreal
        8
    forreal  
    OP
       2014-05-29 18:59:23 +08:00
    @lynx
    ceil函数貌似是向上近似,不是四舍五入
    import math
    a = math.ceil(0.4)
    print(a)
    结果也是1
    hahastudio
        9
    hahastudio  
       2014-05-29 21:05:52 +08:00   3
    Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import decimal
    >>> a = decimal.Decimal("0.5")
    >>> decimal.getcontext().rounding = decimal.ROUND_UP
    >>> print round(a)
    1.0

    感觉 @riaqn 是正解,是Python 3的round函数对策略做了调整
    这个取整是“四舍六入五留双” http://zh.wikipedia.org/zh-cn/%E6%95%B0%E5%80%BC%E4%BF%AE%E7%BA%A6
    我记得第一次见到这种取整规则是在大物实验上= =
    rrfeng
        10
    rrfeng  
       2014-05-29 21:26:16 +08:00
    math 里好像有来着
    forreal
        11
    forreal  
    OP
       2014-05-30 00:21:00 +08:00 via iPad
    @hahastudio
    涨知识了
    SimbaPeng
        12
    SimbaPeng  
       2017-07-13 12:50:57 +08:00
    上面都没说到关键点,你这个代码绝对可以实现标准的四舍五入不丢失精度,不过为什么你没有得到 1,是因为 round 函数如果省略掉第二个参数将会永远返回 int 类型,而你的 a 是 Decimal 对象,所以你要这样写才能得到 1:
    print(round(a, 0))
    linhua
        13
    linhua  
       2017-07-18 16:07:05 +08:00
    @SimbaPeng
    这个是 向上取整, 你可以试试 0.1
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2590 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 14:53 PVG 22:53 LAX 06:53 JFK 09:53
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86