什么是“%”操作符是什么意思? [英] What does the '%' operator mean?

查看:367
本文介绍了什么是“%”操作符是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码

int a,b,c;
b=1;
c=36;
a=b%c;



什么是%运算符是什么意思?

What does "%" operator mean?

推荐答案

这是在 模(或模)运算符

It is the modulo (or modulus) operator:

模运算符(%)由除以第二第一个操作数之后,计算剩余

The modulus operator (%) computes the remainder after dividing its first operand by its second.

例如:

class Program
{
    static void Main()
    {
        Console.WriteLine(5 % 2);       // int
        Console.WriteLine(-5 % 2);      // int
        Console.WriteLine(5.0 % 2.2);   // double
        Console.WriteLine(5.0m % 2.2m); // decimal
        Console.WriteLine(-5.2 % 2.0);  // double
    }
}



示例输出:

Sample output:

1
-1
0.6
0.6
-1.2

请注意,运算符的结果等于 X - (X / Y)* Y 键,如果为零, DivideByZeroException 被抛出。

Note that the result of the % operator is equal to x – (x / y) * y and that if y is zero, a DivideByZeroException is thrown.

如果 X 都是非整数值 X%Y 计算为 X - N * Y ,其中 N 是最大可能整数小于或等于 X / Y (更多详细信息,在 C#4.0规格一节的 7.8.3余数运算符)。

If x and y are non-integer values x % y is computed as x – n * y, where n is the largest possible integer that is less than or equal to x / y (more details in the C# 4.0 Specification in section 7.8.3 Remainder operator).

有关进一步的细节,你可能想的例子看看相应的维基百科文章:

For further details and examples you might want to have a look at the corresponding Wikipedia article:

模操作 (维基百科)

Modulo operation (on Wikipedia)

这篇关于什么是“%”操作符是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆