Share via


Operador / (Referencia de C#)

El operador de división (/) permite dividir su primer operando por su segundo operando. Todos los tipos numéricos poseen operadores de división predefinidos.

Comentarios

Los tipos definidos por el usuario pueden sobrecargar el operador / (vea operador). Una sobrecarga del operador /, sobrecarga implícitamente el operador /=.

El resultado de dividir dos enteros siempre es un número entero. Por ejemplo, el resultado de 7 / 3 es 2. Para determinar el resto de 7 / 3, utilice el operador de resto (%). Para obtener un cociente como un número racional o fracción, asigne al dividendo o al divisor el tipo float o el tipo double. Puede asignar al tipo de forma implícita si express el dividendo o el divisor como decimal, poniendo un dígito a la derecha del separador decimal, como se muestra en el ejemplo siguiente.

Ejemplo

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

Vea también

Referencia

operadores de C#

Conceptos

Guía de programación de C#

Otros recursos

Referencia de C#