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

四捨五入為最接近數位的策略,而當數位在兩個數字之間半形時,它會四捨五入到最接近零的數位。

ToEven 0

四捨五入為最接近數位的策略,當數位在兩個數字之間半形時,它會四捨五入到最接近的偶數。

ToNegativeInfinity 3

向下舍入的策略,其結果最接近且不大於無限精確結果。

ToPositiveInfinity 4

向上導向舍入的策略,其結果最接近且不小於無限精確的結果。

ToZero 2

將四捨五入為零的策略,其結果最接近且大小不大於無限精確的結果。

範例

下列範例示範 Math.Round 方法與 MidpointRounding 列舉:

decimal result;

// Round a positive value using different strategies.
// The precision of the result is 1 decimal place.

result = Math.Round(3.45m, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({3.45m}, 1, MidpointRounding.ToEven)");
result = Math.Round(3.45m, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({3.45m}, 1, MidpointRounding.AwayFromZero)");
result = Math.Round(3.47m, 1, MidpointRounding.ToZero);
Console.WriteLine($"{result} = Math.Round({3.47m}, 1, MidpointRounding.ToZero)\n");

// Round a negative value using different strategies.
// The precision of the result is 1 decimal place.

result = Math.Round(-3.45m, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({-3.45m}, 1, MidpointRounding.ToEven)");
result = Math.Round(-3.45m, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({-3.45m}, 1, MidpointRounding.AwayFromZero)");
result = Math.Round(-3.47m, 1, MidpointRounding.ToZero);
Console.WriteLine($"{result} = Math.Round({-3.47m}, 1, MidpointRounding.ToZero)\n");

/*
This code example produces the following results:

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

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
-3.4 = Math.Round(-3.47, 1, MidpointRounding.ToZero)
*/
Dim result As Decimal = 0D
Dim posValue As Decimal = 3.45D
Dim negValue As Decimal = -3.45D

' Round a positive value using different strategies.
' 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)
result = Math.Round(posValue, 1, MidpointRounding.ToZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToZero)",
                   result, posValue)
Console.WriteLine()

' Round a negative value using different strategies.
' 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)
result = Math.Round(negValue, 1, MidpointRounding.ToZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToZero)",
                   result, negValue)
Console.WriteLine()

'This code example produces the following results:
'
'        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.ToZero)
'
'        -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.ToZero)
'

備註

搭配 MidpointRoundingMathF.RoundDecimal.Round 的適當多載 Math.Round 使用 ,以提供更多進位程式的控制權。

有兩個整體四捨五入策略、四捨五入和導向四捨五入,而每個列舉欄位都只參與其中一個策略。

四捨五入至最接近

欄位:

四捨五入到最接近的運算會採用具有隱含或指定有效位數的原始數位;會檢查下一個數位,也就是該有效位數加上一個;和 會傳回與原始數位相同的精確度最接近的數位。 如果是正數,如果下一個數位是從 0 到 4,則最接近的數位會接近負無限大。 如果下一個數位是從 6 到 9,則最接近的數位會接近正無限大。 針對負數,如果下一個數位是從 0 到 4,則最接近的數位會接近正無限大。 如果下一個數位是從 6 到 9,則最接近的數位會接近負無限大。

如果下一個數位是從 0 到 4 或 6 到 9, MidpointRounding.AwayFromZeroMidpointRounding.ToEven 不會影響舍入作業的結果。 不過,如果下一個數位是 5,這是兩個可能結果之間的中間點,而所有剩餘的數位都是零,或沒有剩餘的數位,則最接近的數位模棱兩可。 在此情況下,中的 MidpointRounding 四捨五入模式可讓您指定舍入作業會傳回最接近的零或最接近偶數的數位。

下表示范將一些負數和正數四捨五入的結果,以及四捨五入到最接近的模式。 用來四捨五入數位的有效位數為零,這表示小數點之後的數位會影響四捨五入作業。 例如,針對數位 -2.5,小數點後面的數位為 5。 因為該數位是中間點,所以您可以使用 MidpointRounding 值來判斷舍入的結果。 如果 AwayFromZero 指定 ,則會傳回 -3,因為它是離零最近的數位,且有效位數為零。 如果 ToEven 指定 ,則會傳回 -2,因為它是精確度為零的最接近偶數。

原始數位 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

方向四捨五入

欄位:

導向四捨五入作業會採用具有隱含或指定有效位數的原始數位,並以與原始數位相同的有效位數,以特定方向傳回下一個最接近的數位。 控制執行 MidpointRounding 舍入的預先定義數位的導向模式。

下表示范將一些負數和正數舍入與導向舍入模式的結果。 用來四捨五入數位的有效位數為零,這表示小數點之前的數位會受到四捨五入運算的影響。

原始數位 ToNegativeInfinity ToPositiveInfinity ToZero
3.5 3 4 3
2.8 2 3 2
2.5 2 3 2
2.1 2 3 2
-2.1 -3 -2 -2
-2.5 -3 -2 -2
-2.8 -3 -2 -2
-3.5 -4 -3 -3

適用於