Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to round in multiples of 0.25 [duplicate]

I need to round the currency amount in 0.25, 0.50, 0.75 and if greater than 0.75, it must round to the next integer.

How to do it?

Example need to round:

  • 25.91 to 26,
  • 25.21 to 25.25
  • 25.44 to 25.50

and so on.

like image 857
vbt Avatar asked Oct 16 '25 15:10

vbt


1 Answers

If you want to round to the next highest quarter, you can use 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

If you want to round to the closest quarter instead of the next highest, just replace math.ceil with round:

>>> def nearest_quarter(x):
...     return round(x*4)/4
...
>>> nearest_quarter(4.51)
4.5
like image 128
user3483203 Avatar answered Oct 18 '25 12:10

user3483203



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!