JavaScript:向上取整至下一个5的倍数

188

我需要一个实用函数,它接受一个整数值(长度从2到5位数),将其向上舍入到下一个5的倍数,而不是最近的5的倍数。这是我的代码:

function round5(x)
{
    return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
}
当我运行round5(32)时,它给了我30,但我希望得到35
当我运行round5(37)时,它给了我35,但我希望得到40
当我运行round5(132)时,它给了我130,但我希望得到135
当我运行round5(137)时,它给了我135,但我希望得到140
等等...
如何做到这一点?

3
round5(5) 应该返回 5 还是 10? - user2357112
1
怎么样:将x除以5,向上取整到最近的整数(使用Math.ceil函数),然后乘以5? - Martin Wilson
2
round5(5) 应该返回 5。 - Amit Erandole
10个回答

422
这将完成工作:
function round5(x)
{
    return Math.ceil(x / 5) * 5;
}

这只是常见的将number四舍五入到最接近的x的倍数的函数Math.round(number/x)*x的变体,但使用.ceil代替.round使其始终向上舍入,而不是根据数学规则向下/向上舍入。


1
你能解释一下你是怎么这么快就想到了这个解决方案吗?我以为 Math.ceil 只会将小数向上舍入到整数。 - Amit Erandole
2
好的,它在这里四舍五入到整数,@AmitErandole ;) - Michael Krelin - hacker
1
+1 对于紧凑和高效非常有用...这样它会四舍五入到10,对吧? :) - zx81
注意输入 x=0 的情况,这会导致错误。 - Nicolas
4
我很喜欢这个解决方案!我使用闭包来方便地根据需要更改多个内联元素:const roundToNearestMultipleOf = m => n => Math.round(n/m)*m 使用方法:roundToNearestMultipleOf(5)(32) - gfullam
显示剩余4条评论

45
const roundToNearest5 = x => Math.round(x / 5) * 5

这将把数字四舍五入到最近的5。要始终向上舍入到最近的5,请使用Math.ceil。同样,要始终向下舍入,请使用Math.floor而不是Math.round。 然后,您可以像调用任何其他函数一样调用此函数。例如,

roundToNearest5(21)

将返回:

20

+1 是因为我需要一种四舍五入到最近的五的方法。(OP要求向上四舍五入到下一个五。因此,被接受的答案确实是正确的,@Oliver。) - AlexG
只是好奇,为什么不使用 Math.round() - Kunal Tanwar

9
像这样吗?
function roundup5(x) { return (x%5)?x-x%5+5:x }

1
感谢您的解决方案,我的独特情况需要一个四舍五入,它不使用浮点数运算,但可以使用模运算。 - DoomGoober

4

我在搜索类似内容时来到了这里。 如果我的数是—0、—1、—2,它应该向下取整为—0;如果是—3、—4、—5,它应该向上取整为—5。

我想出了这个解决方案:

function round(x) { return x % 5 < 3 ? (x % 5 === 0 ? x : Math.floor(x / 5) * 5) : Math.ceil(x / 5) * 5 }

还有测试:

for (var x = 40; x < 51; x++) {
  console.log(x+"=>", x % 5 < 3 ? (x % 5 === 0 ? x : Math.floor(x / 5) * 5) : Math.ceil(x / 5) * 5)
}
// 40 => 40
// 41 => 40
// 42 => 40
// 43 => 45
// 44 => 45
// 45 => 45
// 46 => 45
// 47 => 45
// 48 => 50
// 49 => 50
// 50 => 50

2
可以更简单地使用 Math.round 来实现这个。 - Spencer Stolworthy

3
voici 2 solutions possibles :
y= (x % 10==0) ? x : x-x%5 +5; //......... 15 => 20 ; 37 => 40 ;  41 => 45 ; 20 => 20 ; 

z= (x % 5==0) ? x : x-x%5 +5;  //......... 15 => 15 ; 37 => 40 ;  41 => 45 ; 20 => 20 ;

问候
保罗


1

无需使用ifMath,对旧问题进行新答案

x % 5: the remainder
5 - x % 5: the amount need to add up
(5 - x % 5) % 5: make sure it less than 5
x + (5 - x % 5) % 5: the result (x or next multiple of 5)

~~((x + N - 1) / N): equivalent to Math.ceil(x / N)

function round5(x) {
 return x + (5 - x % 5) % 5;
}

function nearest_multiple_of(N, x) {
 return x + (N - x % N) % N;
}

function other_way_nearest_multiple_of(N, x) {
 return ~~((x + N - 1) / N) * N;
}


console.info(nearest_multiple_of(5,    0)); // 0
console.info(nearest_multiple_of(5, 2022)); // 2025
console.info(nearest_multiple_of(5, 2025)); // 2025

console.info(other_way_nearest_multiple_of(5, 2022)); // 2025
console.info(other_way_nearest_multiple_of(5, 2025)); // 2025


-1
我用一个while循环解决了这个问题,或者使用其他类型的循环也可以。重要的是要增加一个数字n,直到n % 5 == 0; 就像这样。
while(n % 5 != 0) {
    n++;
}
return n;

-1
const fn = _num =>{
    return Math.round(_num)+ (5 -(Math.round(_num)%5))
}

使用round的原因是预期输入可能是随机数。
谢谢!

-1

// 精度四舍五入

var round = function (value, precision) {
    return Math.round(value * Math.pow(10, precision)) / Math.pow(10, precision);
};

// 精确到小数点后 5 位四舍五入

var round5 = (value, precision) => {
    return round(value * 2, precision) / 2;
}

-3
if( x % 5 == 0 ) {
    return int( Math.floor( x / 5 ) ) * 5;
} else {
    return ( int( Math.floor( x / 5 ) ) * 5 ) + 5;
}

可能吗?


1
ReferenceError: int 未定义。也许您想使用 parseInt,但这并不必要,因为 Math.floor 返回一个数字。 - pawel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接