Round a variable up to the next closest multiple of X

前端 未结 3 1355
时光取名叫无心
时光取名叫无心 2021-02-19 07:43

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\         


        
3条回答
  •  天涯浪人
    2021-02-19 08:36

    function NearestMultiple(i, j) {
        alert(Math.ceil(i/ j) * j);
    }
    
    NearestMultiple(1007, 250); //returns 1250
    

    See example at http://jsfiddle.net/SUya9/1/

    Or what James said too!

    EDIT: I see you wanted to round up all the time...Updated fiddle, but James got her in 1.

提交回复
热议问题