将变量舍入到X的下一个最接近的倍数 [英] Round a variable up to the next closest multiple of X
问题描述
我正在寻找一种方法将数字四舍五入到250的下一个最接近的倍数。例如,如果我有以下JS:
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();
...我们想象containerHeight的价值是680px,我希望有办法最高为750px(如果值为1007,则应该最多为1250)。我怀疑这需要一个比我预期的更复杂的解决方案。或者jQuery有一个内置函数可以使这个可行吗?
...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?
我想这更像是一个数学问题,而不是一个jQuery问题(但我的jQuery语法知识)也有点限制:)
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!
推荐答案
containerHeight = Math.ceil(containerHeight / 250.0) * 250;
这篇关于将变量舍入到X的下一个最接近的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!