MidpointRounding Enumeración

# MidpointRounding (Enumeración)

.NET Framework (current version)

Especifica cómo los métodos de redondeo matemáticos deben procesar un número que está comprendido entre dos números.

Espacio de nombres:   System

## Sintaxis

```[ComVisibleAttribute(true)]
public enum MidpointRounding
```

## Miembros

Nombre de miembroDescripción
AwayFromZero

Cuando un número está comprendido entre otros dos, se redondea hacia el número más cercano y más alejado de cero.

ToEven

Cuando un número está comprendido entre otros dos, se redondea hacia el número par más cercano.

## Comentarios

Use MidpointRounding with appropriate overloads of Math.Round to provide more control of the rounding process.

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.For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity.If the next digit is from 6 through 9, the nearest number is toward positive infinity.For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity.If the next digit is from 6 through 9, the nearest number is toward negative infinity.

In the previous cases, the MidpointRounding enumeration does not affect the result of the rounding operation.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.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.

The following table demonstrates the results of rounding some negative and positive numbers in conjunction with the values of MidpointRounding.The precision used to round the numbers is zero, which means the number after the decimal point affects the rounding operation.For example, for the number -2.5, the digit after the decimal point is 5.Because that digit is the midpoint, you can use a MidpointRounding value to determine the result of rounding.If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero.If ToEven is specified, -2 is returned because it is the nearest even number with a precision of zero.

Original number

AwayFromZero

ToEven

3.5

4

4

2.8

3

3

2.5

3

2

2.1

2

2

-2.1

-2

-2

-2.5

-3

-2

-2.8

-3

-3

-3.5

-4

-4

## Ejemplos

The following code example demonstrates the Round method in conjunction with the MidpointRounding enumeration.

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

class Sample
{
public static void Main()
{
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)

*/
```

## Información de versión

Universal Windows Platform
Disponible desde 4.5
.NET Framework
Disponible desde 2.0
Portable Class Library
Compatible con: portable .NET platforms
Windows Phone Silverlight
Disponible desde 8.0
Windows Phone
Disponible desde 8.1

## Vea también

Volver al principio
Mostrar: