Share via


/ 演算子 (C# リファレンス)

除算演算子 (/)、最初のオペランドが 2 番目のオペランドで除算します。すべての数値型には定義済みの除算演算子があります。

解説

/ 演算子はユーザー定義型でオーバーロードできます。詳細については、「operator」を参照してください。/ 演算子をオーバーロードすると、/= 演算子が暗黙的にオーバーロードされます。

2 つの整数を除算したときの結果は常に整数になります。7 の結果は、/2 は 3 です。7 の残りの部分を決定するのには/3、剰余演算子を使用 (%)。商を有理数または小数として得るには、非除数または除数を float 型または double 型にします。次の例のように、小数点の右側にある数字を入れて配当金または除数としては、10 進数を表す場合は、暗黙的に型を割り当てることができます。

使用例

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# リファレンス