javascript 检查值是否是 10 的倍数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15618062/
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
Check if value is multiple of 10
提问by user1937021
I want to check if a value is a multiple of a certain number, for example, multiples of 10, but I also want to be able to change it to whatever I want.
我想检查一个值是否是某个数字的倍数,例如 10 的倍数,但我也希望能够将其更改为我想要的任何值。
if (directWinner == 10){
}
回答by adeneo
You'd use the modulus operator for that :
您可以为此使用模数运算符:
if (directWinner % 10 === 0){
directWinner = 20;
}
Added a small dose of jQuery for no good reason at all ?
无缘无故地添加了少量的 jQuery?
$.modu = function(check, against) {
return check % against === 0;
}
if ( $.modu(directWinner, 10) ) {
directWinner = 20;
}
回答by Bergi
回答by Alnitak
Use the modulo operator (assuming positive integers) :
使用模运算符(假设为正整数):
if (directWinner % 10 === 0) {
...
}