【发布时间】:2015-02-11 17:52:34
【问题描述】:
我正在使用Flask framework 构建一个金融网站,目前正在为其编写单元测试。我正在使用Babel package 到format money amounts,我遇到了相当奇怪的舍入行为。我希望在5 的情况下四舍五入,或者至少保持一致。但是看看这个:
>>> from decimal import Decimal
>>> from babel.numbers import format_currency
>>> print format_currency(Decimal('.235'), 'EUR', locale='nl_NL')
€ 0,24
>>> print format_currency(Decimal('.245'), 'EUR', locale='nl_NL')
€ 0,24
为什么会这样,更重要的是;我该如何解决?
ps:我希望.245 向上舍入到.25
[编辑]
我去寻找the source,它链接到some other 件of code。但我无法真正弄清楚那里出了什么问题以及为什么它似乎随机向上或向下取整。有人知道吗?
【问题讨论】:
-
听起来像是银行家的四舍五入:en.wikipedia.org/wiki/Rounding#Round_half_to_even
标签: python decimal rounding currency python-babel