# MidpointRounding 枚举

## 定义

``public enum class MidpointRounding``
``public enum MidpointRounding``
``````[System.Runtime.InteropServices.ComVisible(true)]
public enum MidpointRounding``````
``type MidpointRounding = ``
``````[<System.Runtime.InteropServices.ComVisible(true)>]
type MidpointRounding = ``````
``Public Enum MidpointRounding``

MidpointRounding

## 字段

 AwayFromZero 1 “舍入到最接近的值”模式：当一个数字是其他两个数字的中间值时，会将其舍入为两个值中绝对值较小的值。Round to nearest mode: when a number is halfway between two others, it is rounded toward the nearest number that is away from zero. ToEven 0 “舍入到最接近的值”模式：当一个数字是其他两个数字的中间值时，会将其舍入为最接近的偶数。Round to nearest mode: when a number is halfway between two others, it is rounded toward the nearest even number. ToNegativeInfinity 3 “定向”模式：将数字向下舍入，并且结果最接近且不大于无限精确的结果。Directed mode: the number is rounded down, with the result closest to and no greater than the infinitely precise result. ToPositiveInfinity 4 “定向”模式：将数字向上舍入，并且结果最接近且不小于无限精确的结果。Directed mode: the number is rounded up, with the result closest to and no less than the infinitely precise result. ToZero 2 “定向”模式：将数字向零舍入，并且结果最接近且不大于无限精确的结果。Directed mode: the number is rounded toward zero, with the result closest to and no greater in magnitude than the infinitely precise result.

## 示例

``````// 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 positiveValue = 3.45m;
decimal negativeValue = -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(positiveValue, 1);
Console.WriteLine(\$"{result} = Math.Round({positiveValue}, 1)");
result = Math.Round(negativeValue, 1);
Console.WriteLine(\$"{result} = Math.Round({negativeValue}, 1)");
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(positiveValue, 1, MidpointRounding.ToEven);
Console.WriteLine(\$"{result} = Math.Round({positiveValue}, 1, MidpointRounding.ToEven)");
result = Math.Round(positiveValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine(\$"{result} = Math.Round({positiveValue}, 1, MidpointRounding.AwayFromZero)");
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(negativeValue, 1, MidpointRounding.ToEven);
Console.WriteLine(\$"{result} = Math.Round({negativeValue}, 1, MidpointRounding.ToEven)");
result = Math.Round(negativeValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine(\$"{result} = Math.Round({negativeValue}, 1, MidpointRounding.AwayFromZero)");
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)
'
``````

## 注解

`MidpointRounding`与适当重载一起使用 Math.Round ，以更好地控制舍入过程。Use `MidpointRounding` with appropriate overloads of Math.Round to provide more control of the rounding process.

#### 舍入到最接近Round to nearest

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

### 定向舍入Directed rounding

2.82.8 22 33 22
2.52.5 22 33 22
2.12.1 22 33 22
-2.1-2.1 -3-3 -2-2 -2-2
-2.5-2.5 -3-3 -2-2 -2-2
-2.8-2.8 -3-3 -2-2 -2-2