需要以0.25的倍数进行舍入

vbt*_*vbt 3 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

等等.

use*_*203 8

如果你想要四舍五入到下一个最高的季度,你可以使用math.ceil().

>>> import math
>>> def quarter(x):
...     return math.ceil(x*4)/4
...
>>> quarter(25.91)
26.0
>>> quarter(25.21)
25.25
>>> quarter(25.44)
25.5
Run Code Online (Sandbox Code Playgroud)

如果你想要舍入到最接近的季度而不是下一个最高的季度,只需替换math.ceilround:

>>> def nearest_quarter(x):
...     return round(x*4)/4
...
>>> nearest_quarter(4.51)
4.5
Run Code Online (Sandbox Code Playgroud)