Round a variable up to the next closest multiple of X

前端 未结 3 1375
时光取名叫无心
时光取名叫无心 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.

    0 讨论(0)
  • 2021-02-19 08:42
    containerHeight = Math.ceil(containerHeight / 250.0) * 250;
    
    0 讨论(0)
  • 2021-02-19 08:44

    simple

    var rounded = Math.ceil(value / round) * round;
    
    0 讨论(0)
提交回复
热议问题