1

I am trying to extract the round up and round down of given values to the nearest 0.25 multiples. I have tried using round, ceil and floor functions but not getting the desired output. Is there any other function I can use to achieve the expected output shared in the below code snippet.

import math
x=[0.14,2.31,1.56,1.98,0.12,0.11,0.13,0.25]
lup=[]
ldown=[]
base=0.25
for i in x:
    x1= base*(round(float(i/base)))
    x2= base*(math.floor(float(i/base)))
    lup.append(x1)
    ldown.append(x2)
    # print(F"value:{i},fraction:{float(i/base)},fround:{round(float(i/base))},lup:{x1},ldown:{x2}")
print(F"lup={lup}")
print(F"ldown={ldown}")

expected output:

    lup = [0.25,2.5,1.75,2.0,0.25,0.25,0.25]
    ldown=[0,2.25,1.5,1.75,0.0,0.0,0.0,0.25]

obtained output:

lup=[0.25, 2.25, 1.5, 2.0, 0.0, 0.0, 0.25, 0.25]
ldown=[0.0, 2.25, 1.5, 1.75, 0.0, 0.0, 0.0, 0.25]
srinivas
  • 301
  • 1
  • 9

1 Answers1

1

Use math.ceil(The opposite of math.floor) instead of math.round:

x1= base*(math.ceil(float(i/base)))
sagi
  • 40,026
  • 6
  • 59
  • 84