MidpointRounding MidpointRounding MidpointRounding MidpointRounding Enum

Definition

Gibt an, wie mathematische Rundungsmethoden eine Zahl verarbeiten sollen, die genau zwischen zwei Zahlen liegt.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
Vererbung
MidpointRoundingMidpointRoundingMidpointRoundingMidpointRounding
Attribute

Felder

AwayFromZero AwayFromZero AwayFromZero AwayFromZero 1

Wenn eine Zahl genau zwischen zwei Ganzzahlen liegt, wird sie auf die nächste größere Ganzzahl aufgerundet.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

Wenn eine Zahl genau zwischen zwei Ganzzahlen liegt, wird sie auf die nächste kleinere Ganzzahl abgerundet.When a number is halfway between two others, it is rounded toward the nearest even number.

ToNegativeInfinity ToNegativeInfinity ToNegativeInfinity ToNegativeInfinity 3
ToPositiveInfinity ToPositiveInfinity ToPositiveInfinity ToPositiveInfinity 4
ToZero ToZero ToZero ToZero 2

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, die Round -Methode in Verbindung mit der ToEven Enumeration.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)
'

Hinweise

Verwendung MidpointRounding mit entsprechenden Überladungen der Math.Round mehr Kontrolle über den Rundungsmodus Prozess bereitstellen.Use MidpointRounding with appropriate overloads of Math.Round to provide more control of the rounding process.

Einen lässt es sich um eine ursprüngliche Anzahl mit impliziten oder der angegebenen Genauigkeit; untersucht die nächste Ziffer, die am Genauigkeit plus eins ist. und die nächste Zahl mit der gleichen Genauigkeit als die ursprüngliche Anzahl zurückgibt.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. Für positive Zahlen ist die nächste Ziffer zwischen 0 und 4, die nächste Zahl in Richtung minus unendlich.For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity. Die nächste Ziffer ist 6 bis 9, die nächste Zahl in Richtung plus unendlich.If the next digit is from 6 through 9, the nearest number is toward positive infinity. Für negative Zahlen ist die nächste Ziffer zwischen 0 und 4, die nächste Zahl in Richtung plus unendlich.For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity. Die nächste Ziffer ist 6 bis 9, die nächste Zahl in Richtung minus unendlich.If the next digit is from 6 through 9, the nearest number is toward negative infinity.

In den vorherigen Fällen die MidpointRounding Enumeration wirkt sich nicht auf das Ergebnis der Rundung.In the previous cases, the MidpointRounding enumeration does not affect the result of the rounding operation. Allerdings ist, wird die nächste Ziffer 5, ist der Mitte zwischen zwei möglichen Ergebnissen, und alle verbleibende Ziffern sind 0 (null), oder es werden keine verbleibenden Ziffern, die nächste Zahl mehrdeutig.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. In diesem Fall die MidpointRounding -Enumeration können Sie angeben, ob die Rundung auf die nächste Zahl von 0 (null) oder die nächste gerade Zahl zurückgegeben.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.

Die folgende Tabelle zeigt die Ergebnisse der Rundung einige negativen und positiven Zahlen in Verbindung mit den Werten der MidpointRounding.The following table demonstrates the results of rounding some negative and positive numbers in conjunction with the values of MidpointRounding. Die Genauigkeit verwendet, um die Zahlen zu runden ist 0 (null), d. h., nach dem Dezimaltrennzeichen an den Rundungsvorgang.The precision used to round the numbers is zero, which means the number after the decimal point affects the rounding operation. Beispielsweise ist die Ziffer nach dem Dezimaltrennzeichen für die Anzahl-2.5, 5.For example, for the number -2.5, the digit after the decimal point is 5. Da diese Ziffer in der Mitte liegt, können Sie mithilfe einer MidpointRounding Wert das Ergebnis der Abrundung fest.Because that digit is the midpoint, you can use a MidpointRounding value to determine the result of rounding. Wenn AwayFromZero angegeben wird, wird die-3 zurückgegeben, da es sich um die nächste Zahl von 0 (null) mit einer Genauigkeit von 0 (null) ist.If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. Wenn ToEven angegeben wird, wird die-2 zurückgegeben, da es sich um die nächste gerade Zahl mit einer Genauigkeit von 0 (null) ist.If ToEven is specified, -2 is returned because it is the nearest even number with a precision of zero.

Ursprüngliche AnzahlOriginal 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

Gilt für: