MidpointRounding 列挙体
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(True)> _ Public Enumeration MidpointRounding

メンバ名 | 説明 | |
---|---|---|
AwayFromZero | 数値が 2 つの数値の中間に位置するときに、ゼロから遠い方の近似値に丸められます。 | |
ToEven | 数値が 2 つの数値の中間に位置するときに、最も近い偶数方向に丸められます。 |

MidpointRounding を System.Math.Round の適切なオーバーロードと共に使用することにより、丸め処理をより詳細に制御できます。
丸め処理を行うメソッドで、2 つの数値の中間に位置する数値を処理する方法は一定ではありません。たとえば、2.5 は 2 と 3 の中間にあります。MidpointRounding は、偶数方向に丸めるかどうかを指定します。偶数方向に丸める場合、この例では 2 が生成されます。ゼロから遠い方に丸める場合は 3 が生成されます。
次の表は、いくつかの正数および負数、およびこれらを MidpointRounding で丸めた結果を示しています。

MidpointRounding 列挙体と組み合わせて Round メソッドを使用するコード例を次に示します。
' 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) '
// 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. 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) */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- MidpointRounding 列挙体のページへのリンク