Javascript:四舍五入到 5 的下一个倍数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18953384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Round up to the next multiple of 5
提问by Amit Erandole
I need a utility function that takes in an integer value (ranging from 2 to 5 digits in length) that rounds up to the nextmultiple of 5 instead of the nearestmultiple of 5. Here is what I got:
我需要一个实用函数,它接受一个整数值(长度为 2 到 5 位数字),该值向上取整为 5 的下一个倍数而不是最接近的 5 的倍数。这是我得到的:
function round5(x)
{
return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
}
When I run round5(32)
, it gives me 30
, where I want 35.
When I run round5(37)
, it gives me 35
, where I want 40.
当我跑步时round5(32)
,它给了30
我想要的 35。
当我跑步时round5(37)
,它给了35
我想要的 40。
When I run round5(132)
, it gives me 130
, where I want 135.
When I run round5(137)
, it gives me 135
, where I want 140.
当我跑步时round5(132)
,它给了130
我想要的 135。
当我跑步时round5(137)
,它给了135
我想要的 140。
etc...
等等...
How do I do this?
我该怎么做呢?
回答by pawel
This will do the work:
这将完成以下工作:
function round5(x)
{
return Math.ceil(x/5)*5;
}
It's just a variation of the common rounding number
to nearest multiple of x
function Math.round(number/x)*x
, but using .ceil
instead of .round
makes it always round up instead of down/up according to mathematical rules.
它只是普通四舍五入number
到最接近的x
函数倍数的一种变体Math.round(number/x)*x
,但使用.ceil
而.round
不是根据数学规则使其始终向上舍入而不是向下/向上舍入。
回答by Spencer Stolworthy
const roundToNearest5 = x => Math.round(x/5)*5
This will round the number to the nearest 5. To always round up to the nearest 5, use Math.ceil
. Likewise, to always round down, use Math.floor
instead of Math.round
.
You can then call this function like you would any other. For example,
这会将数字四舍五入到最接近的 5。要始终四舍五入到最接近的 5,请使用Math.ceil
。同样,要始终向下舍入,请使用Math.floor
代替Math.round
。然后,您可以像调用其他函数一样调用此函数。例如,
roundToNearest5(21)
will return:
将返回:
20
回答by AymKdn
I arrived here while searching for something similar. If my number is —0, —1, —2 it should floor to —0, and if it's —3, —4, —5 it should ceil to —5.
我是在寻找类似的东西时到达这里的。如果我的号码是-0、-1、-2,它应该下限到-0,如果是-3、-4、-5,它应该下限到-5。
I came up with this solution:
我想出了这个解决方案:
function round(x) { return x%5<3 ? (x%5===0 ? x : Math.floor(x/5)*5) : Math.ceil(x/5)*5 }
And the tests:
和测试:
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
回答by Michael Krelin - hacker
Like this?
像这样?
function roundup5(x) { return (x%5)?x-x%5+5:x }
回答by Paul Neulat
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 ;
Regards Paul
问候保罗
回答by Dennis T
// round with precision
// 精确舍入
var round = function (value, precision) {
return Math.round(value * Math.pow(10, precision)) / Math.pow(10, precision);
};
// round to 5 with precision
// 精确四舍五入到 5
var round5 = (value, precision) => {
return round(value * 2, precision) / 2;
}
回答by Parit
const fn = _num =>{
return Math.round(_num)+ (5 -(Math.round(_num)%5))
}
reason for using round is that expected input can be a random number.
使用 round 的原因是预期输入可以是随机数。
Thanks!!!
谢谢!!!
回答by MyFantasy512
if( x % 5 == 0 ) {
return int( Math.floor( x / 5 ) ) * 5;
} else {
return ( int( Math.floor( x / 5 ) ) * 5 ) + 5;
}
maybe?
或许?