Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round a variable up to the next closest multiple of X

I'm looking for a way to round up a number to the next closest multiple of 250. So for example if I had the following JS:

var containerHeight = $("#container").height();

...And we imagine the value of "containerHeight" was 680px, I would want a way to round up to 750px (if the value was 1007, it should round up to 1250). I suspect this requires a solution that is more complex than I anticipate. Or perhaps jQuery has a built in function that will make this feasible?

I suppose this is more of a math question than it is a jQuery question (but my jQuery syntax knowledge is also a bit limited :)

Any ideas / bits of help are greatly appreciated, Thanks!

like image 266
LearnWebCode Avatar asked Feb 14 '12 16:02

LearnWebCode


People also ask

How do you round a number to the nearest multiple?

The MROUND function rounds a number to the nearest given multiple. The multiple to use for rounding is provided as the significance argument. If the number is already an exact multiple, no rounding occurs and the original number is returned.

How do you round to the nearest multiple in Python?

Introduction to the Python round() functionThe round() function rounds the number to the closest multiple of 10-ndigits. In other words, the round() function returns the number rounded to ndigits precision after the decimal point. If ndigits is omitted or None , the round() will return the nearest integer.

How do you round to the nearest multiple of 5?

If you need to round a number to the nearest multiple of 5, you can use the MROUND function and supply 5 for number of digits. The value in B6 is 17 and the result is 15 since 15 is the nearest multiple of 5 to 17.

How do you find the next multiple of 5 in Python?

Divide the number by 5. Call round(a_number) on the result a_number to get an int . Multiply that int by 5 to get the nearest multiple.


1 Answers

containerHeight = Math.ceil(containerHeight / 250.0) * 250;
like image 100
James McLaughlin Avatar answered Sep 22 '22 20:09

James McLaughlin