MidpointRounding 列挙型
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値を丸めるために数学的丸めメソッドで使用する必要がある戦略を指定します。
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
- 継承
- 属性
フィールド
AwayFromZero | 1 | 最も近い数値に丸める戦略で、他の 2 つの数値の中間にある場合は、0 から離れた最も近い数値に丸められます。 |
ToEven | 0 | 最も近い数値に丸める戦略であり、他の 2 つの数値の中間にある場合は、最も近い偶数に丸められます。 |
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)
'
注釈
、、 のMath.RoundMathF.RoundDecimal.Round適切なオーバーロードと共に を使用MidpointRounding
して、丸め処理をより詳細に制御します。
最も近い四捨五入とダイレクト丸めという 2 つの全体的な丸め戦略があり、各列挙フィールドは、これらの戦略の 1 つに参加します。
最も近いに丸める
フィールド:
最も近い丸め操作は、暗黙的または指定された有効桁数を持つ元の数値を受け取ります。は、次の桁を調べます。これは、その有効桁数に 1 を加えたものになります。と は、元の数値と同じ有効桁数で最も近い数値を返します。 正の数値の場合、次の桁が 0 から 4 の場合、最も近い数値は負の無限大に向かってです。 次の桁が 6 から 9 の場合、最も近い数値は正の無限大に向かいます。 負の数の場合、次の桁が 0 から 4 の場合、最も近い数値は正の無限大に向かいます。 次の桁が 6 から 9 の場合、最も近い数値は負の無限大に向かいます。
次の桁が 0 から 4 または 6 から 9 の場合、 MidpointRounding.AwayFromZero
と MidpointRounding.ToEven
は丸め操作の結果には影響しません。 ただし、次の数字が 5 で、2 つの結果の中間点であり、残りのすべての数字が 0 であるか、残りの数字がない場合、最も近い数値はあいまいです。 この場合、 の MidpointRounding
最も近い四捨五入モードを使用すると、丸め操作で 0 から最も近い数値を返すか、最も近い偶数から返すかを指定できます。
次の表は、一部の負と正の数値を、最も近い丸めモードと組み合わせて丸める結果を示しています。 数値の丸め精度は 0 です。これは、小数点の後の数値が丸め操作に影響することを意味します。 たとえば、数値 -2.5 の場合、小数点の後の数字は 5 です。 その数字は中点であるため、値を MidpointRounding
使用して丸めの結果を決定できます。 が指定されている場合 AwayFromZero
、-3 が返されます。これは、有効桁数が 0 の 0 から最も近い数値であるためです。 が指定されている場合 ToEven
、-2 が返されます。これは、有効桁数が 0 の最も近い偶数であるためです。
元の番号 | 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
。
次の表は、一部の負の数値と正の数値を、方向丸めモードと組み合わせて丸めた結果を示しています。 数値の丸め精度は 0 です。これは、小数点の前の数値が丸め操作の影響を受ける可能性があることを意味します。
元の番号 | 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 |