MidpointRounding MidpointRounding MidpointRounding MidpointRounding Enum

定義

数値の丸め処理を行うメソッドで、2 つの数値の中間に位置する数値を処理する方法を指定します。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
継承
MidpointRoundingMidpointRoundingMidpointRoundingMidpointRounding
属性

フィールド

AwayFromZero AwayFromZero AwayFromZero AwayFromZero 1

数値が 2 つの数値の中間に位置するときに、ゼロから遠い方の近似値に丸められます。When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

ToEven ToEven ToEven ToEven 0

数値が 2 つの数値の中間に位置するときに、最も近い偶数方向に丸められます。When a number is halfway between two others, it is rounded toward the nearest even number.

ToNegativeInfinity ToNegativeInfinity ToNegativeInfinity ToNegativeInfinity 3

数値が他の 2 つの数値の中間に位置するとき、無限精度の結果に最も近く、それより大きくない結果に丸められます。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 ToPositiveInfinity ToPositiveInfinity ToPositiveInfinity 4

数値が他の 2 つの数値の中間に位置するとき、無限精度の結果に最も近く、それより小さくない結果に丸められます。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 ToZero ToZero ToZero 2

数値が他の 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.

次の例ではMath.Round 、メソッドをMidpointRounding列挙体と組み合わせて使用する方法を示します。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 'Main
End Class 'Sample
'
'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.

丸め演算では、暗黙的または指定された有効桁数を使用して元の数値を取得します。その有効桁数に1を加えた次の数字を調べます。とは、元の数値と同じ有効桁数を持つ最も近い数値を返します。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で、これが2つの結果の中間点であり、残りのすべての桁が0であるか、残りの桁がない場合は、最も近い数値があいまいになります。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列挙体を使用すると、丸め処理で最も近い数値を0から、または最も近い偶数の値に返すかどうかを指定できます。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. 数値の丸めに使用される精度は0です。これは、小数点の後の数値が丸め演算に影響することを意味します。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 が返されます。これは、有効桁数が0の0から遠い方の数値であるためです。If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. ToEven指定した場合、-2 が返されます。これは、有効桁数が0の最も近い偶数であるためです。If ToEven is specified, -2 is returned because it is the nearest even number with a precision of zero.

元の番号Original 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

適用対象