Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a floor function with a "step" argument

I would like to create a function floor(number, step), which acts like :

floor(0, 1) = 0
floor(1, 1) = 1
floor(1, 2) = 0
floor(5, 2) = 4
floor(.8, .25) = .75

What is the better way to do something like that ?

Thanks.

like image 668
Ttuff Avatar asked Jan 21 '23 14:01

Ttuff


2 Answers

You could do something like floor( val / step ) * step

like image 164
x4u Avatar answered Jan 29 '23 06:01

x4u


what you want is basically the same as

step * (x // step)

isn't ?

like image 26
mykhal Avatar answered Jan 29 '23 07:01

mykhal