/ 运算符(C# 参考)

除法运算符 (/) 将通过第二个操作数的第一个操作数。 所有数值类型都具有预定义的除法运算符。

备注

用户定义的类型可重载 / 运算符(请参见运算符)。 对 / 运算符进行重载将隐式重载 /= 运算符

两个整数相除的结果始终为一个整数。 例如,7 的结果 / 3 是 2。 要确定 7 的其余部分 / 3,使用余数运算符 (%)。 若要获取作为有理数或分数的商,应将被除数或除数设置为 float 类型或 double 类型。 如果您通过将数字放到小数点的右侧,如以下示例所示表示被除数或除数为小数,您可以隐式指定类型。

示例

class Division
{
    static void Main()
    {        
        Console.WriteLine("\nDividing 7 by 3.");
        // Integer quotient is 2, remainder is 1.
        Console.WriteLine("Integer quotient:           {0}", 7 / 3);
        Console.WriteLine("Negative integer quotient:  {0}", -7 / 3);
        Console.WriteLine("Remainder:                  {0}", 7 % 3);
        // Force a floating point quotient.
        float dividend = 7;
        Console.WriteLine("Floating point quotient:    {0}", dividend / 3);

        Console.WriteLine("\nDividing 8 by 5.");
        // Integer quotient is 1, remainder is 3.
        Console.WriteLine("Integer quotient:           {0}", 8 / 5);
        Console.WriteLine("Negative integer quotient:  {0}", 8 / -5);
        Console.WriteLine("Remainder:                  {0}", 8 % 5);
        // Force a floating point quotient.
        Console.WriteLine("Floating point quotient:    {0}", 8 / 5.0);
    }
}
// Output:
//Dividing 7 by 3.
//Integer quotient:           2
//Negative integer quotient:  -2
//Remainder:                  1
//Floating point quotient:    2.33333333333333

//Dividing 8 by 5.
//Integer quotient:           1
//Negative integer quotient:  -1
//Remainder:                  3
//Floating point quotient:    1.6

请参阅

C# 参考

C# 编程指南

C# 运算符