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.

次のコード例に示します、Roundメソッドと組み合わせて、ToEven列挙体。The following code example demonstrates the Round method in conjunction with the ToEven 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)

*/
// 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)

*/
' This example demonstrates the Math.Round() method in conjunction 
' with the MidpointRounding enumeration.
Imports System

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. 有効桁数の数値を丸めるに使用では、小数点 10 進数の影響、丸め処理後の数を意味する 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 が返されます。If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. ToEven が指定されている場合は、最も近い偶数の桁数が 0 である-2 が返されます。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

適用対象