MidpointRounding 列舉

定義

指定數學的四捨五入方法在遇到剛好位於兩個數字中間的數字時,應採用何種處理方式。Specifies how mathematical rounding methods should process a number that is midway between two numbers.

public enum class MidpointRounding
public enum MidpointRounding
[System.Runtime.InteropServices.ComVisible(true)]
public enum MidpointRounding
type MidpointRounding = 
Public Enum MidpointRounding
繼承
MidpointRounding
屬性

欄位

AwayFromZero 1

當某個數字剛好位於另外兩個數字之間的中間時,將其四捨五入為離零距離最近的數字。When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

ToEven 0

當某個數字剛好位於另外兩個數字之間的中點時,將其四捨五入為距離最近的偶數。When a number is halfway between two others, it is rounded toward the nearest even number.

ToNegativeInfinity 3

當某個數字剛好位於另外兩個數字之間的中點時,將其捨入成為距離最近的結果,且不大於無限精確的結果。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

當某個數字剛好位於另外兩個數字之間的中點時,將其捨入成為距離最近的結果,且不小於無限精確的結果。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

當某個數字剛好位於另外兩個數字之間的中點時,將其捨入成為距離最近的結果,且大小不大於無限精確的結果。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.

範例

下列範例會示範如何搭配 MidpointRounding 列舉來使用 Math.Round 方法: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)
'

備註

使用 MidpointRounding 搭配適當的 Math.Round 多載,以提供更多的舍入程式控制權。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. 對於正數,如果下一個數位是從0到4,則最接近的數位會向負無限大。For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity. 如果下一個數位是從6到9,則最接近的數位會向正無限大。If the next digit is from 6 through 9, the nearest number is toward positive infinity. 若為負數,如果下一個數位是從0到4,則最接近的數位會向正無限大。For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity. 如果下一個數位是從6到9,則最接近的數位會向負無限大。If the next digit is from 6 through 9, the nearest number is toward negative infinity.

在先前的案例中,MidpointRounding 列舉不會影響舍入運算的結果。In the previous cases, the MidpointRounding enumeration does not affect the result of the rounding operation. 不過,如果下一個數位是5,也就是兩個可能結果之間的中點,而且所有剩餘的數位都是零或沒有剩餘的數位,則最接近的數位會是不明確的。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. 在此情況下,MidpointRounding 列舉可讓您指定舍入作業是否傳回離零的最接近數位或最接近的偶數。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.

下表示范將一些負數和正數與 MidpointRounding的值進行舍入的結果。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. 例如,對於數位-2.5,小數點後面的位數為5。For example, for the number -2.5, the digit after the decimal point is 5. 因為該數位是中間點,所以您可以使用 MidpointRounding 值來判斷舍入的結果。Because that digit is the midpoint, you can use a MidpointRounding value to determine the result of rounding. 如果指定了 AwayFromZero,則會傳回-3,因為它是離零且精確度為零的最近數位。If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. 如果指定了 ToEven,則會傳回-2,因為它是最接近有效位數為零的偶數。If ToEven is specified, -2 is returned because it is the nearest even number with a precision of zero.

原始數位Original number Midpointrounding.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

適用於