我想找到一个更接近特定数字的数字。在当前示例中,分数为 15。我将把 n 传递给方法,它将返回最接近的完全除以 15 的值 我想要一个这样的方法
function getCloser(no){
if(no is closer to 0) // in this case no can be 1,2,3,4,5,6,7
return 0
if(no is closer to 15) // in this case no can be 8,9,10,11,12,13,15,16...22
return 15
if(no is closer to 30) // in this case no can be 23 to 37
return 30
and so on...
}
最佳答案
function getCloser(a, b) {
b = b || 15;
return Math.round(a / b) * b;
}
关于javascript - 寻找更接近的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8653362/