다음을 통해 공유


/ 연산자(C# 참조)

나누기 연산자 (/)는 첫째 피연산자에서 둘째 피연산자를 나눕니다.모든 숫자 형식에는 / 연산자가 미리 정의되어 있습니다.

설명

사용자 정의 형식으로 / 연산자를 오버로드할 수 있습니다(operator 참조)./ 연산자를 오버로드하면 /= 연산자도 암시적으로 오버로드됩니다.

두 정수에 대해 나누기를 수행하면 결과는 항상 정수입니다.예를 들어, 결과는 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# 참조