% 在 JavaScript 中做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8900652/
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
What does % do in JavaScript?
提问by fancy
What does the % do in JavaScript?
% 在 JavaScript 中有什么作用?
A definition of what it is and what it does would be much appreciated.
对它是什么以及它做什么的定义将不胜感激。
采纳答案by nulltoken
It's a modulo operator. See this documentationor the specificationfor more information on JavaScript arithmetic operators.
这是一个模运算符。有关JavaScript 算术运算符的更多信息,请参阅此文档或规范。
% (Modulus)
The modulus operator is used as follows:
var1 % var2
The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2. The result will have the same sign as var1; that is, ?1 % 2 returns ?1.
%(模量)
模运算符的使用如下:
变量 1 % 变量 2
取模运算符返回第一个操作数的模第二个操作数,即 var1 modulo var2,在前面的语句中,其中 var1 和 var2 是变量。模函数是 var1 除以 var2 的整数余数。例如,12 % 5 返回 2。结果将与 var1 具有相同的符号;也就是说,?1 % 2 返回?1。
回答by Tushar
ES6 Update:
ES6 更新:
As explained in other answers, it returns the remainder after dividing the dividend by divisor, however this is no longer modulo operator, this is remainder operator.the difference being that the modulo operator result would take the sign of the divisor, not the dividend.Quoted from MDN
正如其他答案中所解释的,它在将被除数除以除数后返回余数,但这不再是模运算符,这是余数运算符。不同之处在于模运算符的结果将采用除数的符号,而不是被除数的符号。引自MDN
The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing
var1
byvar2
— for example —var1 modulo var2
. There is a proposal to get an actual modulo operator in a future version of ECMAScript, the difference being that the modulo operator result would take the sign of the divisor, not the dividend.
当一个操作数除以第二个操作数时,余数运算符返回剩余的余数。它总是采用被除数的符号,而不是除数。它使用一个内置的模函数,以产生结果,这是除以整数余数
var1
通过var2
-例如-var1 modulo var2
。有一个提议在 ECMAScript 的未来版本中获得一个实际的模运算符,不同之处在于模运算符的结果将采用除数的符号,而不是被除数的符号。
Example:
例子:
-10 % 3 // -1
10 % -3 // 1
回答by OpenCoderX
It returns the remainder of a division operation. 5%2
returns 1
它返回除法运算的余数。 5%2
回报1
回答by MHShoman
It is a modulo operator.
It calculates the remainder.
它是一个模运算符。
它计算余数。
If we do 23 % 10,
first, we divide 23 by 10 which equals 2.3
then take .3 * (the divisor) 10
= 3
如果我们做 23 % 10,
首先,我们将 23 除以 10,等于 2.3,
然后取 0.3 *(除数)10
= 3
回答by Justin Niessner
That would be the modulo operator.
那将是模运算符。
It returns the remainder of a division operation:
它返回除法运算的余数:
var remainder = 3 % 2; // equals 1
回答by Romain Bel
Just in case, if someone is looking for a real modulo function (which would always get the sign of the divisor), you can use this:
以防万一,如果有人正在寻找一个真正的模函数(它总是会得到除数的符号),你可以使用这个:
function Modulo(num, denom)
{
if (num%denom >= 0)
{
return Math.abs(num%denom);
}
else
{
return num%denom + denom;
}
}
The Math.abs is to prevent the case -12%12 → -0, which is considered equal to 0 but displayed as -0.
Math.abs 是为了防止出现 -12%12 → -0 的情况,即认为等于 0 但显示为 -0。
回答by Baz1nga
Modulus (%) operator returns the remainder.
模数 (%) 运算符返回余数。
If either value is a string, an attempt is made to convert the string to a number.
如果任一值是字符串,则尝试将字符串转换为数字。
alert(5%3)
will alert 2
alert(5%3)
会提醒 2
回答by Joy Das
% performs as the modulo operator, return division reminder. Example:
% 作为模运算符,返回除法提示。例子:
<script>
var x = 5;
var y = 2;
var z = x % y;
alert(z);
</script>
This will alert 1.
这将提醒 1。
回答by Krishnamoorthy Acharya
Please find the functional usage of the modulus(%) operator.you have an array of 9 names you need to list it in 3 each per line you can use modular operator.
请找到模数(%)运算符的功能用法。您有一个包含 9 个名称的数组,您需要将其列在每行 3 个中,您可以使用模运算符。
listNames=["krishna","vishnu","john","ali","guru","guruvan","tryer","ben"]
var text = "<div class='carousel-inner'>";
text += '<div class="item active">';
for (var i in listNames) {
text += '<div class="div_block" >' + listNames[i] + '</div>';
if (i % 3 == 2) {
text += '</div><div class="item">';
}
}
text += "</div>";
$('#name-list').html(text);