Possible Duplicate:
Round number up to the nearest multiple of 3
I need a javascript call that will round numbers up to the nearest multiple of 12.
Some examples:
1 -> 12
7 -> 12
14 -> 24
27 -> 36
Does anyone know of a good method for doing this? All I can think of is "If between 1-11, make it 12. If between 13-23, make it 24, etc." But that doesn't seem very efficient.
You can use CEILING to round prices, times, instrument readings or any other numeric value. CEILING rounds up using the multiple supplied. You can use the MROUND function to round to the nearest multiple and the FLOOR function to round down to a multiple.
multiple is the multiple to use when rounding. The MROUND function rounds a number to the nearest multiple of the second argument. For example, in the above example, all retail prices have to be rounded to the nearest 5¢. To calculate the retail prices for all products we use this MROUND formula… =MROUND(C4,0.05)
The MROUND function in Excel rounds a given number up or down to the specified multiple. Number - the value you want to round. Multiple - the multiple to which you want to round the number. For example, the formula =MROUND(7, 2) rounds 7 to the nearest multiple of 2 and returns 8 as the result.
Use Math.ceil()
var n = 13;
var next = Math.ceil(n/12) * 12;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With