MidpointRounding Enumeración

Definición

Especifica cómo los métodos de redondeo matemáticos deben procesar un número que está comprendido entre dos números.Specifies how mathematical rounding methods should process a number that is midway between two numbers.

public enum class MidpointRounding
[System.Runtime.InteropServices.ComVisible(true)]
public enum MidpointRounding
type MidpointRounding = 
Public Enum MidpointRounding
Herencia
MidpointRounding
Atributos

Campos

AwayFromZero 1

Cuando un número está comprendido entre otros dos, se redondea hacia el número más cercano y más alejado de cero.When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

ToEven 0

Cuando un número está comprendido entre dos otros, se redondea hacia el número par más cercano.When a number is halfway between two others, it is rounded toward the nearest even number.

ToNegativeInfinity 3

Cuando un número está comprendido entre otros dos, se redondea hacia el resultado más cercano y no mayor que el resultado infinitamente preciso.When a number is halfway between two others, it is rounded toward the result closest to and no greater than the infinitely precise result.

ToPositiveInfinity 4

Cuando un número está comprendido entre otros dos, se redondea hacia el resultado más cercano y no menor que el resultado infinitamente preciso.When a number is halfway between two others, it is rounded toward the result closest to and no less than the infinitely precise result.

ToZero 2

Cuando un número está comprendido entre otros dos, se redondea hacia el resultado más cercano y no mayor en magnitud que el resultado infinitamente preciso.When a number is halfway between two others, it is rounded toward the result closest to and no greater in magnitude than the infinitely precise result.

Ejemplos

En el ejemplo siguiente se Math.Round muestra el método junto con MidpointRounding la enumeración:The following example demonstrates the Math.Round method in conjunction with the MidpointRounding enumeration:

// This example demonstrates the Math.Round() method in conjunction 
// with the MidpointRounding enumeration.
using namespace System;

void main()
{
    Decimal result = (Decimal) 0.0;
    Decimal posValue = (Decimal) 3.45;
    Decimal negValue = (Decimal) -3.45;

    // By default, round a positive and a negative value to the nearest
    // even number. The precision of the result is 1 decimal place.
    result = Math::Round(posValue, 1);
    Console::WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
    result = Math::Round(negValue, 1);
    Console::WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
    Console::WriteLine();

    // Round a positive value to the nearest even number, then to the
    // nearest number away from zero. The precision of the result is 1
    // decimal place.
    result = Math::Round(posValue, 1, MidpointRounding::ToEven);
    Console::WriteLine(
        "{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)",
        result, posValue);
    result = Math::Round(posValue, 1, MidpointRounding::AwayFromZero);
    Console::WriteLine(
        "{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)",
        result, posValue);
    Console::WriteLine();

    // Round a negative value to the nearest even number, then to the
    // nearest number away from zero. The precision of the result is 1
    // decimal place.
    result = Math::Round(negValue, 1, MidpointRounding::ToEven);
    Console::WriteLine(
        "{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)",
        result, negValue);
    result = Math::Round(negValue, 1, MidpointRounding::AwayFromZero);
    Console::WriteLine(
        "{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)",
        result, negValue);
    Console::WriteLine();
}

/*
This code example produces the following results:

3.4 = Math.Round( 3.45, 1)
-3.4 = Math.Round(-3.45, 1)

3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

*/
decimal result = 0.0m;
decimal posValue =  3.45m;
decimal negValue = -3.45m;

// By default, round a positive and a negative value to the nearest even number. 
// The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue);
result = Math.Round(negValue, 1);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue);
Console.WriteLine();

// Round a positive value to the nearest even number, then to the nearest number away from zero. 
// The precision of the result is 1 decimal place.

result = Math.Round(posValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, posValue);
result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, posValue);
Console.WriteLine();

// Round a negative value to the nearest even number, then to the nearest number away from zero. 
// The precision of the result is 1 decimal place.

result = Math.Round(negValue, 1, MidpointRounding.ToEven);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", result, negValue);
result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", result, negValue);
Console.WriteLine();
/*
This code example produces the following results:

3.4 = Math.Round( 3.45, 1)
-3.4 = Math.Round(-3.45, 1)

3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)

*/
' This example demonstrates the Math.Round() method in conjunction 
' with the MidpointRounding enumeration.
Class Sample
    Public Shared Sub Main() 
        Dim result As Decimal = 0D
        Dim posValue As Decimal = 3.45D
        Dim negValue As Decimal = -3.45D
        
        ' By default, round a positive and a negative value to the nearest even number. 
        ' The precision of the result is 1 decimal place.
        result = Math.Round(posValue, 1)
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue)
        result = Math.Round(negValue, 1)
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue)
        Console.WriteLine()
        
        ' Round a positive value to the nearest even number, then to the nearest number 
        ' away from zero. The precision of the result is 1 decimal place.
        result = Math.Round(posValue, 1, MidpointRounding.ToEven)
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", _
                           result, posValue)
        result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero)
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", _
                           result, posValue)
        Console.WriteLine()
        
        ' Round a negative value to the nearest even number, then to the nearest number 
        ' away from zero. The precision of the result is 1 decimal place.
        result = Math.Round(negValue, 1, MidpointRounding.ToEven)
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", _
                            result, negValue)
        result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero)
        Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", _
                           result, negValue)
        Console.WriteLine()
    
    End Sub
End Class
'
'This code example produces the following results:
'
' 3.4 = Math.Round( 3.45, 1)
'-3.4 = Math.Round(-3.45, 1)
'
' 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven)
' 3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero)
'
'-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
'-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
'

Comentarios

Use MidpointRounding con las sobrecargas adecuadas Math.Round de para proporcionar un mayor control del proceso de redondeo.Use MidpointRounding with appropriate overloads of Math.Round to provide more control of the rounding process.

Una operación de redondeo toma un número original con una precisión implícita o especificada; examina el siguiente dígito, que está en esa precisión más uno; y devuelve el número más cercano con la misma precisión que el número original.A rounding operation takes an original number with an implicit or specified precision; examines the next digit, which is at that precision plus one; and returns the nearest number with the same precision as the original number. En el caso de los números positivos, si el siguiente dígito está comprendido entre 0 y 4, el número más cercano es el infinito negativo.For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity. Si el siguiente dígito va de 6 a 9, el número más cercano es el infinito positivo.If the next digit is from 6 through 9, the nearest number is toward positive infinity. En el caso de números negativos, si el siguiente dígito está comprendido entre 0 y 4, el número más cercano es el infinito positivo.For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity. Si el siguiente dígito va de 6 a 9, el número más cercano es el infinito negativo.If the next digit is from 6 through 9, the nearest number is toward negative infinity.

En los casos anteriores, la MidpointRounding enumeración no afecta al resultado de la operación de redondeo.In the previous cases, the MidpointRounding enumeration does not affect the result of the rounding operation. Sin embargo, si el siguiente dígito es 5, que es el punto medio entre dos resultados posibles, y todos los dígitos restantes son cero o no hay ningún dígito restante, el número más cercano es ambiguo.However, if the next digit is 5, which is the midpoint between two possible results, and all remaining digits are zero or there are no remaining digits, the nearest number is ambiguous. En este caso, la MidpointRounding enumeración permite especificar si la operación de redondeo devuelve el número más cercano fuera de cero o el número par más cercano.In this case, the MidpointRounding enumeration enables you to specify whether the rounding operation returns the nearest number away from zero or the nearest even number.

En la tabla siguiente se muestran los resultados de redondear algunos números positivos y negativos junto con los MidpointRoundingvalores de.The following table demonstrates the results of rounding some negative and positive numbers in conjunction with the values of MidpointRounding. La precisión usada para redondear los números es cero, lo que significa que el número después del separador decimal afecta a la operación de redondeo.The precision used to round the numbers is zero, which means the number after the decimal point affects the rounding operation. Por ejemplo, para el número-2,5, el dígito después del separador decimal es 5.For example, for the number -2.5, the digit after the decimal point is 5. Dado que ese dígito es el punto medio, puede usar un MidpointRounding valor para determinar el resultado del redondeo.Because that digit is the midpoint, you can use a MidpointRounding value to determine the result of rounding. Si AwayFromZero se especifica, se devuelve-3 porque es el número más cercano fuera de cero con una precisión de cero.If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. Si ToEven se especifica, se devuelve-2 porque es el número par más cercano con una precisión de cero.If ToEven is specified, -2 is returned because it is the nearest even number with a precision of zero.

Número originalOriginal number AwayFromZeroAwayFromZero ToEvenToEven
3.53.5 44 44
2.82.8 33 33
2.52.5 33 22
2.12.1 22 22
-2.1-2.1 -2-2 -2-2
-2.5-2.5 -3-3 -2-2
-2.8-2.8 -3-3 -3-3
-3.5-3.5 -4-4 -4-4

Se aplica a