Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding number to nearest interval [closed]

Tags:

php

So i have a predefined interval, can be 5, 10, 15, etc

If someone inputs 20 i need it to round up based on the interval. So if it was a 15 minute interval, it would automatically go to 30, if it was a 45 minute interval, it would go to 45

Basically anything <= interval becomes interval anything inbetween intervals becomes the next highest interval

I dont want to write some convoluted php function to do this when there may be a simple way i am just not aware of.

like image 253
Austin Best Avatar asked Mar 08 '13 00:03

Austin Best


3 Answers

Rounding up would be:

$ceiled = $interval * ceil( $value / $interval);
like image 172
Wrikken Avatar answered Oct 02 '22 14:10

Wrikken


roundedInput  = (Ceiling( Input / Interval)) * Interval

So, given an input of 20 and an interval of 15, you'd get:

20/15 = 1.33

1.33 rounded up = 2

2 * 15 = 30

like image 24
frodo2975 Avatar answered Oct 02 '22 14:10

frodo2975


$Rounded = Ceil($value / $interval) * $interval;

like image 26
splash21 Avatar answered Oct 02 '22 13:10

splash21