Python 需要以0.25的倍数舍入

Python 需要以0.25的倍数舍入,python,python-2.7,odoo,odoo-10,Python,Python 2.7,Odoo,Odoo 10,我需要将货币金额四舍五入到0.25、0.50、0.75,如果大于0.75,则必须四舍五入到下一个整数 怎么做 示例需要舍入: 25.91至26 25.21至25.25 25.44至25.50 依此类推。如果你想进入下一个最高季度,你可以使用 如果要舍入到最近的四分之一,而不是下一个最高的四分之一,只需将math.ceil替换为round: >>> def nearest_quarter(x): ... return round(x*4)/4 ... >>

我需要将货币金额四舍五入到0.25、0.50、0.75,如果大于0.75,则必须四舍五入到下一个整数

怎么做

示例需要舍入:

  • 25.91至26
  • 25.21至25.25
  • 25.44至25.50

依此类推。

如果你想进入下一个最高季度,你可以使用


如果要舍入到最近的四分之一,而不是下一个最高的四分之一,只需将
math.ceil
替换为
round

>>> def nearest_quarter(x):
...     return round(x*4)/4
...
>>> nearest_quarter(4.51)
4.5
>>> def nearest_quarter(x):
...     return round(x*4)/4
...
>>> nearest_quarter(4.51)
4.5