Math.Round Yöntem

Tanım

Bir değeri en yakın tamsayıya veya belirtilen kesirli basamak sayısına yuvarlar.Rounds a value to the nearest integer or to the specified number of fractional digits.

Aşırı Yüklemeler

Round(Decimal, Int32, MidpointRounding)

Ondalık değeri belirtilen sayıda kesirli basamağa yuvarlar ve orta nokta değerleri için belirtilen yuvarlama kuralını kullanır.Rounds a decimal value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values.

Round(Double, Int32, MidpointRounding)

Çift duyarlıklı kayan nokta değerini belirtilen sayıda kesirli basamağa yuvarlar ve orta nokta değerleri için belirtilen yuvarlama kuralını kullanır.Rounds a double-precision floating-point value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values.

Round(Double, MidpointRounding)

Bir çift duyarlıklı kayan noktalı değeri en yakın tamsayıya yuvarlar ve orta nokta değerleri için belirtilen yuvarlama kuralını kullanır.Rounds a double-precision floating-point value to the nearest integer, and uses the specified rounding convention for midpoint values.

Round(Double, Int32)

Çift duyarlıklı kayan nokta değerini belirtilen sayıda kesirli basamağa yuvarlar ve orta nokta değerlerini en yakın çift sayıya yuvarlar.Rounds a double-precision floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.

Round(Double)

Çift duyarlıklı kayan noktalı değeri en yakın tamsayı değerine yuvarlar ve orta nokta değerlerini en yakın çift sayıya yuvarlar.Rounds a double-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number.

Round(Decimal, Int32)

Ondalık değeri belirtilen sayıda kesirli basamağa yuvarlar ve orta nokta değerlerini en yakın çift sayıya yuvarlar.Rounds a decimal value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.

Round(Decimal)

Ondalık değeri en yakın tamsayı değerine yuvarlar ve orta nokta değerlerini en yakın çift sayıya yuvarlar.Rounds a decimal value to the nearest integral value, and rounds midpoint values to the nearest even number.

Round(Decimal, MidpointRounding)

Ondalık değeri en yakın tamsayıya yuvarlar ve orta nokta değerleri için belirtilen yuvarlama kuralını kullanır.Rounds a decimal value to the nearest integer, and uses the specified rounding convention for midpoint values.

Örnekler

Açıklamalar bölümündeki örneklere ek olarak, bu makale yöntemin aşağıdaki aşırı yüklerini gösteren örnekler içerir Math.Round :In addition to the examples in the Remarks section, this article includes examples that illustrate the following overloads of the Math.Round method:

Math. Round (ondalık)Math.Round(Decimal)
Math. Round (Double)Math.Round(Double)
Math. Round (Decimal, Int32)Math.Round(Decimal, Int32)
Math. Round (Decimal, MidpointRounding)Math.Round(Decimal, MidpointRounding)
Math. Round (Double, Int32)Math.Round(Double, Int32)
Math. Round (Double, MidpointRounding)Math.Round(Double, MidpointRounding)
Math. Round (Decimal, Int32, MidpointRounding)Math.Round(Decimal, Int32, MidpointRounding)
Math. Round (Double, Int32, MidpointRounding)Math.Round(Double, Int32, MidpointRounding)

Açıklamalar

Bu bölümdeki konular:In this section:

Hangi yöntemi çağırmalıyım?Which method do I call?

Uygun bir yuvarlama yöntemi seçmek için aşağıdaki tabloyu kullanabilirsiniz.You can use the following table to select an appropriate rounding method. Yöntemlere ek olarak Math.Round , ve de içerir Math.Ceiling Math.Floor .In addition to the Math.Round methods, it also includes Math.Ceiling and Math.Floor.

AmaçTo CallCall
En yakın kurala yuvarlama kuralını kullanarak bir sayıyı tamsayıya yuvarlar.Round a number to an integer by using the rounding to nearest convention. Round(Decimal)

-veya--or-

Round(Double)
Belirtilen yuvarlama kuralını kullanarak bir sayıyı tamsayıya yuvarlar.Round a number to an integer by using a specified rounding convention. Round(Decimal, MidpointRounding)

-veya--or-

Round(Double, MidpointRounding)
En yakın kurala yuvarlama kuralını kullanarak bir sayıyı belirtilen sayıda kesirli basamağa yuvarlar.Round a number to a specified number of fractional digits by using the rounding to nearest convention. Round(Decimal, Int32)

-veya--or-

Round(Double, Int32)
Belirtilen bir yuvarlama kuralını kullanarak bir sayıyı belirtilen sayıda kesirli basamağa yuvarlar.Round a number to a specified number of fractional digits by using a specified rounding convention. Round(Decimal, Int32, MidpointRounding)

-veya--or-

Round(Double, Int32, MidpointRounding)
Belirtilen bir Single yuvarlama kuralını kullanarak ve duyarlık kaybını en aza indirerek değeri belirtilen sayıda kesirli basamağa yuvarlar.Round a Single value to a specified number of fractional digits by using a specified rounding convention and minimizing the loss of precision. Öğesini Single bir Decimal ve çağrısına dönüştürün Round(Decimal, Int32, MidpointRounding) .Convert the Single to a Decimal and call Round(Decimal, Int32, MidpointRounding).
Orta nokta değerlerini yuvarlarken duyarlık sorunlarını en aza indirerek, bir sayıyı belirtilen sayıda kesirli basamağa yuvarlar.Round a number to a specified number of fractional digits while minimizing problems of precision in rounding midpoint values. "Büyüktür veya yaklaşık eşittir" karşılaştırması uygulayan bir yuvarlama yöntemi çağırın.Call a rounding method that implements a "greater than or approximately equal to" comparison. Bkz. yuvarlama ve duyarlık.See Rounding and precision.
Kesirli değeri kesirli değerden büyük bir tamsayıya yuvarlar.Round a fractional value to an integer that is greater than the fractional value. Örneğin, 3,1 ile 4 arasında bir yuvarlama yapın.For example, round 3.1 to 4. Ceiling
Kesirli değeri kesirli değerden küçük bir tamsayıya yuvarlar.Round a fractional value to an integer that is less than the fractional value. Örneğin, 3,9 ' i 3 ' e yuvarlayın.For example, round 3.9 to 3. Floor

Orta nokta değerleri ve yuvarlama kurallarıMidpoint values and rounding conventions

Yuvarlama, belirtilen duyarlığa sahip bir sayısal değeri, daha az duyarlığa sahip en yakın değere dönüştürmeyi içerir.Rounding involves converting a numeric value with a specified precision to the nearest value with less precision. Örneğin, Round(Double) 3,4 değerini 3,0 ve Round(Double, Int32) 3,579 değerini 3,58 olarak yuvarlamak için yöntemini kullanabilirsiniz.For example, you can use the Round(Double) method to round a value of 3.4 to 3.0, and the Round(Double, Int32) method to round a value of 3.579 to 3.58.

Orta nokta değerinde, sonuçdaki en az önemli rakamlardan sonraki değer iki sayı arasında tam olarak yarım yoldur.In a midpoint value, the value after the least significant digit in the result is precisely half way between two numbers. Örneğin, 3,47500, iki ondalık basamak olarak yuvarlandığında bir orta nokta değeridir ve bir tamsayıya yuvarlandıysanız 7,500 orta nokta değeridir.For example, 3.47500 is a midpoint value if it is to be rounded two decimal places, and 7.500 is a midpoint value if it is to be rounded to an integer. Bu durumlarda, en yakın değer bir yuvarlama kuralı olmadan kolayca tanımlanamıyor.In these cases, the nearest value can't be easily identified without a rounding convention.

RoundYöntemi, orta nokta değerlerini işlemek için iki yuvarlama kuralı destekler:The Round method supports two rounding conventions for handling midpoint values:

  • Sıfırdan uzağa yuvarlamaRounding away from zero

    Orta nokta değerleri, sıfırdan uzağa doğru bir sonraki sayıya yuvarlanır.Midpoint values are rounded to the next number away from zero. Örneğin 3,75, 3,8 ' e yuvarlanır, 3,85 3,9 ' e yuvarlanır,-3,75-3,8 ' e yuvarlanır ve-3,85-3,9 ' e yuvarlanır.For example, 3.75 rounds to 3.8, 3.85 rounds to 3.9, -3.75 rounds to -3.8, and -3.85 rounds to -3.9. Bu yuvarlama biçimi, MidpointRounding.AwayFromZero sabit listesi üyesi tarafından temsil edilir.This form of rounding is represented by the MidpointRounding.AwayFromZero enumeration member.

    Sıfırdan uzağa yuvarlama, en yaygın olarak tanınan yuvarlama biçimidir.Rounding away from zero is the most widely known form of rounding.

  • En yakın veya Banker yuvarlamaRounding to nearest, or banker's rounding

    Orta nokta değerleri en yakın çift sayıya yuvarlanır.Midpoint values are rounded to the nearest even number. Örneğin, 3,75 ve 3,85 ' nin her ikisi de 3,8 ve-3,75 ve-3,85 ' i-3,8 ' i yuvarlar.For example, both 3.75 and 3.85 round to 3.8, and both -3.75 and -3.85 round to -3.8. Bu yuvarlama biçimi, MidpointRounding.ToEven sabit listesi üyesi tarafından temsil edilir.This form of rounding is represented by the MidpointRounding.ToEven enumeration member.

    En yakın yuvarlama, finansal ve istatistiksel işlemlerde kullanılan standart yuvarlama biçimidir.Rounding to nearest is the standard form of rounding used in financial and statistical operations. IEEE Standart 754, Bölüm 4 ' e uyar.It conforms to IEEE Standard 754, section 4. Birden çok yuvarlama işlemlerinde kullanıldığında, tek bir yönde orta nokta değerlerinin neden olduğu yuvarlama hatasını azaltır.When used in multiple rounding operations, it reduces the rounding error that is caused by consistently rounding midpoint values in a single direction. Bazı durumlarda, bu yuvarlama hatası önemli olabilir.In some cases, this rounding error can be significant.

    Aşağıdaki örnek, tek bir yönde orta nokta değerlerinin sürekli olarak yuvarlanması ile sonuçlanabileceği sapması gösterir.The following example illustrates the bias that can result from consistently rounding midpoint values in a single direction. Örnek, bir değer dizisinin gerçek Decimal dünyayı hesaplar ve dizideki değerler iki kural kullanılarak yuvarlanarak ortalama değeri hesaplar.The example computes the true mean of an array of Decimal values, and then computes the mean when the values in the array are rounded by using the two conventions. Bu örnekte, doğru ortalama ve en yakın yuvarlama olduğunda sonucun anlamı aynıdır.In this example, the true mean and the mean that results when rounding to nearest are the same. Ancak, sıfırdan uzağa yuvarlama 05 (veya% 3,6) farklılık gösterdiğinde sonuç olarak gelir doğru bir şekilde.However, the mean that results when rounding away from zero differs by .05 (or by 3.6%) from the true mean.

    decimal[] values = { 1.15m, 1.25m, 1.35m, 1.45m, 1.55m, 1.65m };
    decimal sum = 0;
    
    // Calculate true mean.
    foreach (var value in values)
       sum += value;
    
    Console.WriteLine("True mean:     {0:N2}", sum/values.Length);
    
    // Calculate mean with rounding away from zero.
    sum = 0;
    foreach (var value in values)
       sum += Math.Round(value, 1, MidpointRounding.AwayFromZero);
    
    Console.WriteLine("AwayFromZero:  {0:N2}", sum/values.Length);
    
    // Calculate mean with rounding to nearest.
    sum = 0;
    foreach (var value in values)
       sum += Math.Round(value, 1, MidpointRounding.ToEven);
    
    Console.WriteLine("ToEven:        {0:N2}", sum/values.Length);
    // The example displays the following output:
    //       True mean:     1.40
    //       AwayFromZero:  1.45
    //       ToEven:        1.40
    
    Module Example
       Public Sub Main()
          Dim values() As Decimal = { 1.15d, 1.25d, 1.35d, 1.45d, 1.55d, 1.65d }
          Dim sum As Decimal
          
          ' Calculate true mean.
          For Each value In values
             sum += value
          Next
          Console.WriteLine("True mean:     {0:N2}", sum/values.Length)
          
          ' Calculate mean with rounding away from zero.
          sum = 0
          For Each value In values
             sum += Math.Round(value, 1, MidpointRounding.AwayFromZero)
          Next
          Console.WriteLine("AwayFromZero:  {0:N2}", sum/values.Length)
          
          ' Calculate mean with rounding to nearest.
          sum = 0
          For Each value In values
             sum += Math.Round(value, 1, MidpointRounding.ToEven)
          Next
          Console.WriteLine("ToEven:        {0:N2}", sum/values.Length)
       End Sub
    End Module
    ' The example displays the following output:
    '       True mean:     1.40
    '       AwayFromZero:  1.45
    '       ToEven:        1.40
    

Varsayılan olarak, Round yöntemi en yakın kurala yuvarlama kullanır.By default, the Round method uses the rounding to nearest convention. Aşağıdaki tabloda, Round yönteminin ve her birinin kullandığı yuvarlama kuralının aşırı yüklemeleri listelenmektedir.The following table lists the overloads of the Round method and the rounding convention that each uses.

YüklemekOverload Yuvarlama kuralıRounding convention
Round(Decimal) ToEven
Round(Double) ToEven
Round(Decimal, Int32) ToEven
Round(Double, Int32) ToEven
Round(Decimal, MidpointRounding) Parametreye göre belirlenir mode .Determined by mode parameter.
Round(Double, MidpointRounding) Parametreye göre belirlenir modeDetermined by mode parameter
Round(Decimal, Int32, MidpointRounding) Parametreye göre belirlenir modeDetermined by mode parameter
Round(Double, Int32, MidpointRounding) Parametreye göre belirlenir modeDetermined by mode parameter

Yuvarlama ve duyarlıkRounding and precision

Bir yuvarlama işleminin orta nokta değeri içerip içermediğini anlamak için, bu yöntem, Round özgün değeri 10 nile yuvarlayarak çarpar, burada n , dönüş değerindeki istenen kesirli basamak sayısıdır ve değerin kalan kesirli kısmının. 5 ' e eşit veya ondan büyük olup olmadığını belirler.In order to determine whether a rounding operation involves a midpoint value, the Round method multiplies the original value to be rounded by 10 n, where n is the desired number of fractional digits in the return value, and then determines whether the remaining fractional portion of the value is greater than or equal to .5. Bu, eşitlik için bir testte bulunan hafif bir çeşitdir ve başvuru konusunun "eşitlik için test etme" bölümünde anlatıldığı gibi Double , kayan nokta değerleriyle eşitlik için testler, ikili gösterimle ve duyarlıkla kayan nokta biçiminin sorunları nedeniyle sorunlu olur.This is a slight variation on a test for equality, and as discussed in the "Testing for Equality" section of the Double reference topic, tests for equality with floating-point values are problematic because of the floating-point format's issues with binary representation and precision. Bu, 5 ' den biraz daha az olan (duyarlık kaybı nedeniyle) bir sayının herhangi bir kesirli kısmının yukarı yuvarolmayacağı anlamına gelir.This means that any fractional portion of a number that is slightly less than .5 (because of a loss of precision) will not be rounded upward.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

Aşağıdaki örnek, sorunu gösterir.The following example illustrates the problem. Bu, tekrar tekrar 11,0 'e ekler ve sonucu en yakın tamsayıya yuvarlar.It repeatedly adds .1 to 11.0 and rounds the result to the nearest integer. Yuvarlama kuralı ne olursa olsun, 11,5, 12 ' ye yuvarlanacak.Regardless of the rounding convention, 11.5 should round to 12. Ancak, örneğin çıktısında gösterildiği gibi, değildir.However, as the output from the example shows, it does not. Örnek, kayan nokta değerinin tam hassasiyetini göstermek için "R" Standart sayısal biçim dizesini kullanır ve yinelenen eklemeler sırasında yuvarlanacak değerin doğruluğu kaybettiğini ve değeri gerçekten 11.499999999999998 olduğunu gösterir.The example uses the "R" standard numeric format string to display the floating point value's full precision, and shows that the value to be rounded has lost precision during repeated additions, and its value is actually 11.499999999999998. .499999999999998, 5 değerinden küçük olduğundan, değer bir sonraki en yüksek tamsayıya yuvarlanır.Because .499999999999998 is less than .5, the value is not rounded to the next highest integer. Örnekte de gösterildiği gibi, yalnızca 11,5 sabit değerini bir değişkene atamanız durumunda bu sorun oluşmaz Double .As the example also shows, this problem does not occur if we simply assign the constant value 11.5 to a Double variable.

using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("{0,5} {1,20:R}  {2,12} {3,15}\n",
                        "Value", "Full Precision", "ToEven",
                        "AwayFromZero");
      double value = 11.1;
      for (int ctr = 0; ctr <= 5; ctr++)
         value = RoundValueAndAdd(value);

      Console.WriteLine();

      value = 11.5;
      RoundValueAndAdd(value);
   }

   private static double RoundValueAndAdd(double value)
   {
      Console.WriteLine("{0,5:N1} {0,20:R}  {1,12} {2,15}",
                        value, Math.Round(value, MidpointRounding.ToEven),
                        Math.Round(value, MidpointRounding.AwayFromZero));
      return value + .1;
   }
}
// The example displays the following output:
//       Value       Full Precision        ToEven    AwayFromZero
//
//        11.1                 11.1            11              11
//        11.2                 11.2            11              11
//        11.3   11.299999999999999            11              11
//        11.4   11.399999999999999            11              11
//        11.5   11.499999999999998            11              11
//        11.6   11.599999999999998            12              12
//
//        11.5                 11.5            12              12
Module Example
   Public Sub Main()
      Dim value As Double = 11.1

      Console.WriteLine("{0,5} {1,20:R}  {2,12} {3,15}", 
                        "Value", "Full Precision", "ToEven",
                        "AwayFromZero")
      Console.WriteLine()
      For ctr As Integer = 0 To 5    
         value = RoundValueAndAdd(value)
      Next
      Console.WriteLine()

      value = 11.5
      RoundValueAndAdd(value)
   End Sub
   
   Private Function RoundValueAndAdd(value As Double) As Double
      Console.WriteLine("{0,5:N1} {0,20:R}  {1,12} {2,15}", 
                        value, Math.Round(value, MidpointRounding.ToEven),
                        Math.Round(value, MidpointRounding.AwayFromZero))
      Return value + .1
   End Function   
End Module
' The example displays the following output:
'       Value       Full Precision        ToEven    AwayFromZero
'       
'        11.1                 11.1            11              11
'        11.2                 11.2            11              11
'        11.3   11.299999999999999            11              11
'        11.4   11.399999999999999            11              11
'        11.5   11.499999999999998            11              11
'        11.6   11.599999999999998            12              12
'       
'        11.5                 11.5            12              12

Orta nokta değerlerinde duyarlık sorunları en büyük olasılıkla aşağıdaki koşullarda ortaya çıkar:Problems of precision in rounding midpoint values are most likely to arise in the following conditions:

Yuvarlama işlemlerinde duyarlık olmaması sorunlu durumlarda şunları yapabilirsiniz:In cases where the lack of precision in rounding operations is problematic, you can do the following:

  • Yuvarlama işlemi bir değeri yuvarlar bir aşırı yüklemeyi çağırırsa, Double öğesini bir değere değiştirebilir Double Decimal ve bunun yerine bir değeri yuvarlar bir aşırı yüklemeyi çağırabilirsiniz Decimal .If the rounding operation calls an overload that rounds a Double value, you can change the Double to a Decimal value and call an overload that rounds a Decimal value instead. DecimalVeri türü de temsilde ve duyarlık kaybı ile ilgili sorunlar olsa da, bu sorunlar çok daha az yaygındır.Although the Decimal data type also has problems of representation and loss of precision, these issues are far less common.

  • Yuvarlanacak değerin bir orta nokta değeri için kabul edilebilir olup olmadığını belirlemek üzere "neredeyse eşit" test gerçekleştiren bir özel yuvarlama algoritması tanımlayın.Define a custom rounding algorithm that performs a "nearly equal" test to determine whether the value to be rounded is acceptably close to a midpoint value. Aşağıdaki örnek, bir RoundApproximate kesirli değerin orta nokta yuvarlama değerine tabi olması için bir orta nokta değerinin yeterince yakınına doğru olup olmadığını inceleyen bir yöntemi tanımlar.The following example defines a RoundApproximate method that examines whether a fractional value is sufficiently near to a midpoint value to be subject to midpoint rounding. Örneğin çıkışının gösterdiği gibi, önceki örnekte gösterilen yuvarlama sorununu düzeltir.As the output from the example shows, it corrects the rounding problem shown in the previous example.

    :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.math.round.overload/cs/precision2.cs" interactive="try-dotnet" id="Snippet8":::
    :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round.overload/vb/precision2.vb" id="Snippet8":::
    

Yuvarlama ve tek duyarlıklı kayan nokta değerleriRounding and single-precision floating-point values

RoundYöntemi, ve türündeki bağımsız değişkenleri kabul eden aşırı yüklemeleri içerir Decimal Double .The Round method includes overloads that accept arguments of type Decimal and Double. Tür değerlerini yuvarlatılmış Yöntem yok Single .There are no methods that round values of type Single. Yöntemin aşırı yüklerinden Single birine bir değer geçirirseniz Round , (C# ' ta) veya dönüştürülür (Visual Basic olarak) Double ve Round bir parametreye karşılık gelen aşırı yükleme Double çağrılır.If you pass a Single value to one of the overloads of the Round method, it is cast (in C#) or converted (in Visual Basic) to a Double, and the corresponding Round overload with a Double parameter is called. Bu bir genişletme dönüştürmesi olsa da, aşağıdaki örnekte gösterildiği gibi genellikle duyarlık kaybı vardır.Although this is a widening conversion, it often involves a loss of precision, as the following example illustrates. SingleYöntemine 16,325 değeri geçirildiğinde Round ve en yakın kurala yuvarlama kullanılarak iki ondalık basamağa yuvarlandığında, sonuç 16,33 ' 16,32 nin beklenen sonucunu değil, ' dir.When a Single value of 16.325 is passed to the Round method and rounded to two decimal places using the rounding to nearest convention, the result is 16.33 and not the expected result of 16.32.

using System;

public class Example
{
   public static void Main()
   {
      Single value = 16.325f;
      Console.WriteLine("Widening Conversion of {0:R} (type {1}) to {2:R} (type {3}): ",
                        value, value.GetType().Name, (double) value,
                        ((double) (value)).GetType().Name);
      Console.WriteLine(Math.Round(value, 2));
      Console.WriteLine(Math.Round(value, 2, MidpointRounding.AwayFromZero));
      Console.WriteLine();

      Decimal decValue = (decimal) value;
      Console.WriteLine("Cast of {0:R} (type {1}) to {2} (type {3}): ",
                        value, value.GetType().Name, decValue,
                        decValue.GetType().Name);
      Console.WriteLine(Math.Round(decValue, 2));
      Console.WriteLine(Math.Round(decValue, 2, MidpointRounding.AwayFromZero));
   }
}
// The example displays the following output:
//    Widening Conversion of 16.325 (type Single) to 16.325000762939453 (type Double):
//    16.33
//    16.33
//
//    Cast of 16.325 (type Single) to 16.325 (type Decimal):
//    16.32
//    16.33
Module Example
   Public Sub Main()
      Dim value As Single = 16.325
      Console.WriteLine("Widening Conversion of {0:R} (type {1}) to {2:R} (type {3}): ", 
                        value, value.GetType().Name, CDbl(value), 
                        CDbl(value).GetType().Name)
      Console.WriteLine(Math.Round(value, 2))
      Console.WriteLine(Math.Round(value, 2, MidpointRounding.AwayFromZero))
      Console.WriteLine()
      
      Dim decValue As Decimal = CDec(value)
      Console.WriteLine("Cast of {0:R} (type {1}) to {2} (type {3}): ", 
                        value, value.GetType().Name, decValue, 
                        decValue.GetType().Name)
      Console.WriteLine(Math.Round(decValue, 2))
      Console.WriteLine(Math.Round(decValue, 2, MidpointRounding.AwayFromZero))
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'    Widening Conversion of 16.325 (type Single) to 16.325000762939453 (type Double):
'    16.33
'    16.33
'    
'    Cast of 16.325 (type Single) to 16.325 (type Decimal):
'    16.32
'    16.33

Bu beklenmeyen sonuç, değerin ' a dönüştürülürken duyarlık kaybı nedeniyle oluşur Single Double .This unexpected result is due to a loss of precision in the conversion of the Single value to a Double. Double16.325000762939453 'nin sonuç değeri bir orta nokta değeri olmadığından ve 16,325 ' den büyükse, her zaman yukarı yuvarlanır.Because the resulting Double value of 16.325000762939453 is not a midpoint value and is greater than 16.325, it is always rounded upward.

Çoğu durumda, örnekte gösterildiği gibi duyarlık kaybı, değer atama ya da değeri bir değerine dönüştürme yoluyla simge durumuna küçültülmüş ya da ortadan kaldırılabilir Single Decimal .In many cases, as the example illustrates, the loss of precision can be minimized or eliminated by casting or converting the Single value to a Decimal. Bu bir daraltma dönüştürmesi olduğundan, bir atama işlecinin kullanılmasını veya bir dönüştürme yönteminin çağrılmasını unutmayın.Note that, because this is a narrowing conversion, it requires using a cast operator or calling a conversion method.

Round(Decimal, Int32, MidpointRounding)

Ondalık değeri belirtilen sayıda kesirli basamağa yuvarlar ve orta nokta değerleri için belirtilen yuvarlama kuralını kullanır.Rounds a decimal value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values.

public:
 static System::Decimal Round(System::Decimal d, int decimals, MidpointRounding mode);
public static decimal Round (decimal d, int decimals, MidpointRounding mode);
static member Round : decimal * int * MidpointRounding -> decimal
Public Shared Function Round (d As Decimal, decimals As Integer, mode As MidpointRounding) As Decimal

Parametreler

d
Decimal

Yuvarlanacak ondalık bir sayı.A decimal number to be rounded.

decimals
Int32

Dönüş değerindeki ondalık basamak sayısı.The number of decimal places in the return value.

mode
MidpointRounding

dDiğer iki sayı arasında bir Midway olursa yuvarlama belirtimi.Specification for how to round d if it is midway between two other numbers.

Döndürülenler

Decimal

Öğesine d eşit sayıda kesirli basamak içeren en yakın sayı decimals .The number nearest to d that contains a number of fractional digits equal to decimals. dŞundan daha az kesirli basamak varsa decimals , d değiştirilmez.If d has fewer fractional digits than decimals, d is returned unchanged.

Özel durumlar

decimals 0 ' dan küçük veya 28 ' den büyük.decimals is less than 0 or greater than 28.

mode geçerli bir değeri değil MidpointRounding .mode is not a valid value of MidpointRounding.

Sonuç bir ile aralığının dışındadır Decimal .The result is outside the range of a Decimal.

Açıklamalar

Orta nokta değerleriyle sayıları yuvarlama hakkında bilgi için bkz. orta nokta değerleri ve yuvarlama kuralları .See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

decimalsBağımsız değişkenin değeri 0 ile 28 arasında değişebilir.The value of the decimals argument can range from 0 to 28.

ÖrnekExample

Aşağıdaki örnek, bu Round yöntemin numaralandırmada nasıl kullanılacağını göstermektedir MidpointRounding .The following example demonstrates how to use the Round method 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 positiveValue = 3.45m;
decimal negativeValue = -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(positiveValue, 1);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1)");
result = Math.Round(negativeValue, 1);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1)");
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(positiveValue, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1, MidpointRounding.ToEven)");
result = Math.Round(positiveValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1, MidpointRounding.AwayFromZero)");
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(negativeValue, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1, MidpointRounding.ToEven)");
result = Math.Round(negativeValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1, MidpointRounding.AwayFromZero)");
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
End Class
'
'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)
'

Ayrıca bkz.

Şunlara uygulanır

Round(Double, Int32, MidpointRounding)

Çift duyarlıklı kayan nokta değerini belirtilen sayıda kesirli basamağa yuvarlar ve orta nokta değerleri için belirtilen yuvarlama kuralını kullanır.Rounds a double-precision floating-point value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values.

public:
 static double Round(double value, int digits, MidpointRounding mode);
public static double Round (double value, int digits, MidpointRounding mode);
static member Round : double * int * MidpointRounding -> double
Public Shared Function Round (value As Double, digits As Integer, mode As MidpointRounding) As Double

Parametreler

value
Double

Yuvarlanacak çift duyarlıklı, kayan noktalı bir sayı.A double-precision floating-point number to be rounded.

digits
Int32

Dönüş değerindeki kesirli basamakların sayısı.The number of fractional digits in the return value.

mode
MidpointRounding

valueDiğer iki sayı arasında bir Midway olursa yuvarlama belirtimi.Specification for how to round value if it is midway between two other numbers.

Döndürülenler

Double

En yakın sayıya value eşit sayıda kesirli basamak vardır digits .The number nearest to value that has a number of fractional digits equal to digits. valueŞundan daha az kesirli basamak varsa digits , value değiştirilmez.If value has fewer fractional digits than digits, value is returned unchanged.

Özel durumlar

digits 0 ' dan küçük veya 15 ' ten büyük.digits is less than 0 or greater than 15.

mode geçerli bir değeri değil MidpointRounding .mode is not a valid value of MidpointRounding.

Açıklamalar

digitsBağımsız değişkenin değeri 0 ile 15 arasında değişebilir.The value of the digits argument can range from 0 to 15. 15 ' in, türün desteklediği en yüksek integral ve kesirli basamak sayısıdır Double .Note that 15 is the maximum number of integral and fractional digits supported by the Double type.

Orta nokta değerleriyle sayıları yuvarlama hakkında bilgi için bkz. orta nokta değerleri ve yuvarlama kuralları .See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

valueBağımsız değişkenin değeri ise, Double.NaN yöntemi döndürür Double.NaN .If the value of the value argument is Double.NaN, the method returns Double.NaN. valueVeya ise Double.PositiveInfinity Double.NegativeInfinity , yöntemi Double.PositiveInfinity sırasıyla veya döndürür Double.NegativeInfinity .If value is Double.PositiveInfinity or Double.NegativeInfinity, the method returns Double.PositiveInfinity or Double.NegativeInfinity, respectively.

ÖrnekExample

Aşağıdaki örnek, bu Round(Double, Int32, MidpointRounding) yöntemin numaralandırmada nasıl kullanılacağını göstermektedir MidpointRounding .The following example demonstrates how to use the Round(Double, Int32, MidpointRounding) method with the MidpointRounding enumeration.

 double posValue =  3.45;
 double negValue = -3.45;

 // Round a positive and a negative value using the default.
 double 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)\n", result, negValue);

 // Round a positive value using a MidpointRounding value.
 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)\n",
                   result, posValue);

 // Round a negative value using a MidpointRounding value.
 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)\n",
                   result, negValue);
// The example displays the following output:
//        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)
Module Example
    Public Sub Main() 
        Dim posValue As Double = 3.45
        Dim negValue As Double = -3.45
        
        ' Round a positive and a negative value using the default.  
        Dim result As Double = 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 using a MidpointRounding value. 
        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 positive value using a MidpointRounding value. 
        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
End Module
' The example displays the following output:
'       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)

Arayanlara Notlar

Bir duyarlık kaybı nedeniyle, ondalık değerleri kayan noktalı sayılar olarak temsil edebilir veya kayan nokta değerlerinde aritmetik işlemler gerçekleştirilemeyebilir, bazı durumlarda Round(Double, Int32, MidpointRounding) yöntem parametresi tarafından belirtilen şekilde orta nokta değerlerini yuvarlatılmış olarak görünmeyebilir mode .Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the Round(Double, Int32, MidpointRounding) method may not appear to round midpoint values as specified by the mode parameter. Bu, aşağıdaki örnekte gösterilmiştir; burada 2,135, 2,14 yerine 2,13 ' e yuvarlanır.This is illustrated in the following example, where 2.135 is rounded to 2.13 instead of 2.14. Bunun nedeni, dahili olarak yöntemin value 10basamaklaçarpdığı ve bu örnekte çarpma işleminin duyarlık kaybını kaybetmesinden kaynaklanır.This occurs because internally the method multiplies value by 10digits, and the multiplication operation in this case suffers from a loss of precision.

::: Code Language = "CSharp" Source = "~/Samples/Snippets/CSharp/VS_Snippets_CLR_System/System.Math.Round/CS/round4.cs" Interactive = "TRY-DotNet-method" ID = "Snippet3":::::: Code Language = "vb" Source = "~/Samples/Snippets/VisualBasic/VS_Snippets_CLR_System/System.Math.Round/vb/round4.vb" ID = "Snippet3"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.math.round/cs/round4.cs" interactive="try-dotnet-method" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/round4.vb" id="Snippet3":::

Ayrıca bkz.

Şunlara uygulanır

Round(Double, MidpointRounding)

Bir çift duyarlıklı kayan noktalı değeri en yakın tamsayıya yuvarlar ve orta nokta değerleri için belirtilen yuvarlama kuralını kullanır.Rounds a double-precision floating-point value to the nearest integer, and uses the specified rounding convention for midpoint values.

public:
 static double Round(double value, MidpointRounding mode);
public static double Round (double value, MidpointRounding mode);
static member Round : double * MidpointRounding -> double
Public Shared Function Round (value As Double, mode As MidpointRounding) As Double

Parametreler

value
Double

Yuvarlanacak çift duyarlıklı, kayan noktalı bir sayı.A double-precision floating-point number to be rounded.

mode
MidpointRounding

valueDiğer iki sayı arasında bir Midway olursa yuvarlama belirtimi.Specification for how to round value if it is midway between two other numbers.

Döndürülenler

Double

En yakın tamsayı value .The integer nearest value. valueİki tamsayının, biri çift ve diğeri de arasında yarı arsa, bunlardan mode hangisinin döndürüleceğini belirler.If value is halfway between two integers, one of which is even and the other odd, then mode determines which of the two is returned. Bu yöntemin bir integral türü yerine bir döndürdüğünü unutmayın Double .Note that this method returns a Double instead of an integral type.

Özel durumlar

mode geçerli bir değeri değil MidpointRounding .mode is not a valid value of MidpointRounding.

Açıklamalar

Orta nokta değerleriyle sayıları yuvarlama hakkında bilgi için bkz. orta nokta değerleri ve yuvarlama kuralları .See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

valueBağımsız değişkenin değeri ise, Double.NaN yöntemi döndürür Double.NaN .If the value of the value argument is Double.NaN, the method returns Double.NaN. valueVeya ise Double.PositiveInfinity Double.NegativeInfinity , yöntemi Double.PositiveInfinity sırasıyla veya döndürür Double.NegativeInfinity .If value is Double.PositiveInfinity or Double.NegativeInfinity, the method returns Double.PositiveInfinity or Double.NegativeInfinity, respectively.

ÖrnekExample

Aşağıdaki örnek, yöntemi tarafından döndürülen değerleri Round(Double) , Round(Double, MidpointRounding) mode bağımsız değişkenine sahip yöntemi ToEven ve Round(Double, MidpointRounding) mode bağımsız değişkenini içeren AwayFromZero yöntemi gösterir.The following example displays values returned by the Round(Double) method, the Round(Double, MidpointRounding) method with a mode argument of ToEven, and the Round(Double, MidpointRounding) method with a mode argument of AwayFromZero.

using System;

public class Example
{
   public static void Main()
   {
      Double[] values = { 12.0, 12.1, 12.2, 12.3, 12.4, 12.5, 12.6,
                          12.7, 12.8, 12.9, 13.0 };
      Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default",
                        "ToEven", "AwayFromZero");
      foreach (var value in values)
         Console.WriteLine("{0,-10:R} {1,-10} {2,-10} {3,-15}",
                           value, Math.Round(value),
                           Math.Round(value, MidpointRounding.ToEven),
                           Math.Round(value, MidpointRounding.AwayFromZero));
   }
}
// The example displays the following output:
//       Value      Default    ToEven     AwayFromZero
//       12         12         12         12
//       12.1       12         12         12
//       12.2       12         12         12
//       12.3       12         12         12
//       12.4       12         12         12
//       12.5       12         12         13
//       12.6       13         13         13
//       12.7       13         13         13
//       12.8       13         13         13
//       12.9       13         13         13
//       13         13         13         13
Module Example
   Public Sub Main()
      Dim values() As Double = { 12.0, 12.1, 12.2, 12.3, 12.4, 12.5, 12.6, 
                                 12.7, 12.8, 12.9, 13.0 }
      Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default", 
                        "ToEven", "AwayFromZero")
      For Each value In values
         Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}",
                           value, Math.Round(value), 
                           Math.Round(value, MidpointRounding.ToEven),
                           Math.Round(value, MidpointRounding.AwayFromZero))
      Next
   End Sub
End Module
' The example displays the following output:
'       Value      Default    ToEven     AwayFromZero
'       12         12         12         12
'       12.1       12         12         12
'       12.2       12         12         12
'       12.3       12         12         12
'       12.4       12         12         12
'       12.5       12         12         13
'       12.6       13         13         13
'       12.7       13         13         13
'       12.8       13         13         13
'       12.9       13         13         13
'       13         13         13         13

Arayanlara Notlar

Bir duyarlık kaybı nedeniyle, ondalık değerleri kayan noktalı sayılar olarak temsil edebilir veya kayan nokta değerlerinde aritmetik işlemler gerçekleştirilemeyebilir, bazı durumlarda Round(Double, MidpointRounding) Yöntem orta nokta değerlerini en yakın çift tamsayıya yuvarlamasına yol açabilir.Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the Round(Double, MidpointRounding) method may not appear to round midpoint values to the nearest even integer. Aşağıdaki örnekte, kayan nokta değerinin bir sonlu ikili temsili olmadığından, Round(Double) 11,5 değeriyle yönteme ilk çağrı 12 yerine 11 döndürür.In the following example, because the floating-point value .1 has no finite binary representation, the first call to the Round(Double) method with a value of 11.5 returns 11 instead of 12.

::: Code Language = "CSharp" Source = "~/Samples/Snippets/CSharp/VS_Snippets_CLR_System/System.Math.Round/CS/round5.cs" Interactive = "TRY-DotNet" ID = "Snippet4":::::: Code Language = "vb" kaynak = "~/Samples/Snippets/VisualBasic/VS_Snippets_CLR_System/System.Math.Round/vb/round5.vb" ID = "Snippet4"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.math.round/cs/round5.cs" interactive="try-dotnet" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/round5.vb" id="Snippet4":::

Ayrıca bkz.

Şunlara uygulanır

Round(Double, Int32)

Çift duyarlıklı kayan nokta değerini belirtilen sayıda kesirli basamağa yuvarlar ve orta nokta değerlerini en yakın çift sayıya yuvarlar.Rounds a double-precision floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.

public:
 static double Round(double value, int digits);
public static double Round (double value, int digits);
static member Round : double * int -> double
Public Shared Function Round (value As Double, digits As Integer) As Double

Parametreler

value
Double

Yuvarlanacak çift duyarlıklı, kayan noktalı bir sayı.A double-precision floating-point number to be rounded.

digits
Int32

Dönüş değerindeki kesirli basamakların sayısı.The number of fractional digits in the return value.

Döndürülenler

Double

Öğesine value eşit sayıda kesirli basamak içeren en yakın sayı digits .The number nearest to value that contains a number of fractional digits equal to digits.

Özel durumlar

digits 0 ' dan küçük veya 15 ' ten büyük.digits is less than 0 or greater than 15.

Açıklamalar

digitsBağımsız değişkenin değeri 0 ile 15 arasında değişebilir.The value of the digits argument can range from 0 to 15. 15 ' in, türün desteklediği en yüksek integral ve kesirli basamak sayısıdır Double .Note that 15 is the maximum number of integral and fractional digits supported by the Double type.

Bu yöntem, varsayılan yuvarlama kuralını kullanır MidpointRounding.ToEven .This method uses the default rounding convention of MidpointRounding.ToEven. Orta nokta değerleriyle sayıları yuvarlama hakkında bilgi için bkz. orta nokta değerleri ve yuvarlama kuralları .See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

valueBağımsız değişkenin değeri ise, Double.NaN yöntemi döndürür Double.NaN .If the value of the value argument is Double.NaN, the method returns Double.NaN. valueVeya ise Double.PositiveInfinity Double.NegativeInfinity , yöntemi Double.PositiveInfinity sırasıyla veya döndürür Double.NegativeInfinity .If value is Double.PositiveInfinity or Double.NegativeInfinity, the method returns Double.PositiveInfinity or Double.NegativeInfinity, respectively.

ÖrnekExample

Aşağıdaki örnek, tek bir kesirli basamak içeren Double değerleri iki kesirli basamakla yuvarlar.The following example rounds double values with two fractional digits to doubles that have a single fractional digit.

Math::Round(3.44, 1); //Returns 3.4.
Math::Round(3.45, 1); //Returns 3.4.
Math::Round(3.46, 1); //Returns 3.5.

Math::Round(4.34, 1); // Returns 4.3
Math::Round(4.35, 1); // Returns 4.4
Math::Round(4.36, 1); // Returns 4.4
Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4.
Math.Round(3.46, 1); //Returns 3.5.

Math.Round(4.34, 1); // Returns 4.3
Math.Round(4.35, 1); // Returns 4.4
Math.Round(4.36, 1); // Returns 4.4
Math.Round(3.44, 1) 'Returns 3.4.
Math.Round(3.45, 1) 'Returns 3.4.
Math.Round(3.46, 1) 'Returns 3.5.

Math.Round(4.34, 1) ' Returns 4.3
Math.Round(4.35, 1) ' Returns 4.4
Math.Round(4.36, 1) ' Returns 4.4

Arayanlara Notlar

Bir duyarlık kaybı nedeniyle, ondalık değerleri kayan noktalı sayılar olarak temsil edebilir veya kayan nokta değerlerinde aritmetik işlemler gerçekleştirilemeyebilir, bazı durumlarda Round(Double, Int32) Yöntem orta nokta değerlerini ondalık konumdaki en yakın çift değere yuvarlar digits .Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the Round(Double, Int32) method may not appear to round midpoint values to the nearest even value in the digits decimal position. Bu, aşağıdaki örnekte gösterilmiştir; burada 2,135, 2,14 yerine 2,13 ' e yuvarlanır.This is illustrated in the following example, where 2.135 is rounded to 2.13 instead of 2.14. Bunun nedeni, dahili olarak yöntemin value 10basamaklaçarpdığı ve bu örnekte çarpma işleminin duyarlık kaybını kaybetmesinden kaynaklanır.This occurs because internally the method multiplies value by 10digits, and the multiplication operation in this case suffers from a loss of precision.

::: Code Language = "CSharp" Source = "~/Samples/Snippets/CSharp/VS_Snippets_CLR_System/System.Math.Round/CS/RounD3.cs" Interactive = "TRY-DotNet" ID = "Snippet2":::::: Code Language = "vb" kaynak = "~/Samples/Snippets/VisualBasic/VS_Snippets_CLR_System/System.Math.Round/vb/RounD3.vb" ID = "Snippet2"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.math.round/cs/round3.cs" interactive="try-dotnet" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/round3.vb" id="Snippet2":::

Ayrıca bkz.

Şunlara uygulanır

Round(Double)

Çift duyarlıklı kayan noktalı değeri en yakın tamsayı değerine yuvarlar ve orta nokta değerlerini en yakın çift sayıya yuvarlar.Rounds a double-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number.

public:
 static double Round(double a);
public static double Round (double a);
static member Round : double -> double
Public Shared Function Round (a As Double) As Double

Parametreler

a
Double

Yuvarlanacak çift duyarlıklı, kayan noktalı bir sayı.A double-precision floating-point number to be rounded.

Döndürülenler

Double

En yakın tamsayı a .The integer nearest a. Kısmi bileşeni a iki tamsayı arasında, biri çift ve diğeri de ise çift sayı döndürülür.If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Bu yöntemin bir integral türü yerine bir döndürdüğünü unutmayın Double .Note that this method returns a Double instead of an integral type.

Açıklamalar

Bu yöntem, varsayılan yuvarlama kuralını kullanır MidpointRounding.ToEven .This method uses the default rounding convention of MidpointRounding.ToEven. Orta nokta değerleriyle sayıları yuvarlama hakkında bilgi için bkz. orta nokta değerleri ve yuvarlama kuralları .See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

aBağımsız değişkenin değeri ise, Double.NaN yöntemi döndürür Double.NaN .If the value of the a argument is Double.NaN, the method returns Double.NaN. aVeya ise Double.PositiveInfinity Double.NegativeInfinity , yöntemi Double.PositiveInfinity sırasıyla veya döndürür Double.NegativeInfinity .If a is Double.PositiveInfinity or Double.NegativeInfinity, the method returns Double.PositiveInfinity or Double.NegativeInfinity, respectively.

Visual Basic 15,8 ' den itibaren, yöntem tarafından döndürülen değeri Round integral dönüştürme işlevlerinegeçirirseniz veya tarafından döndürülen Double değeri Round otomatik olarak Option Strict kapalı olarak bir tamsayıya dönüştürülürse, Double-Integer dönüştürmenin performansı en iyi duruma getirilir.Starting with Visual Basic 15.8, the performance of Double-to-integer conversion is optimized if you pass the value returned by the Round method to the any of the integral conversion functions, or if the Double value returned by Round is automatically converted to an integer with Option Strict set to Off. Bu iyileştirme kodun çok daha hızlı bir şekilde çalışmasını sağlar ve tamsayı türlerine çok sayıda dönüştürme yapan kod için hızlı bir şekilde daha hızlı çalışır.This optimization allows code to run faster -- up to twice as fast for code that does a large number of conversions to integer types. Aşağıdaki örnek, bu tür iyileştirilmiş dönüştürmeleri göstermektedir:The following example illustrates such optimized conversions:

Dim d1 As Double = 1043.75133
Dim i1 As Integer = CInt(Math.Ceiling(d1))        ' Result: 1044

Dim d2 As Double = 7968.4136
Dim i2 As Integer = CInt(Math.Ceiling(d2))        ' Result: 7968

ÖrnekExample

Aşağıdaki örnek, en yakın tamsayı değerine yuvarlama gösterir.The following example demonstrates rounding to the nearest integer value.

using namespace System;

void main()
{
    Console::WriteLine("Classic Math.Round in CPP");
    Console::WriteLine(Math::Round(4.4));     // 4
    Console::WriteLine(Math::Round(4.5));     // 4
    Console::WriteLine(Math::Round(4.6));     // 5
    Console::WriteLine(Math::Round(5.5));     // 6
}
Console.WriteLine("Classic Math.Round in CSharp");
Console.WriteLine(Math.Round(4.4)); // 4
Console.WriteLine(Math.Round(4.5)); // 4
Console.WriteLine(Math.Round(4.6)); // 5
Console.WriteLine(Math.Round(5.5)); // 6
Module Module1

    Sub Main()
    Console.WriteLine("Classic Math.Round in Visual Basic")
    Console.WriteLine(Math.Round(4.4)) ' 4
    Console.WriteLine(Math.Round(4.5)) ' 4
    Console.WriteLine(Math.Round(4.6)) ' 5
    Console.WriteLine(Math.Round(5.5)) ' 6
    End Sub

End Module

Arayanlara Notlar

Bir duyarlık kaybı nedeniyle, ondalık değerleri kayan noktalı sayılar olarak temsil edebilir veya kayan nokta değerlerinde aritmetik işlemler gerçekleştirilemeyebilir, bazı durumlarda Round(Double) Yöntem orta nokta değerlerini en yakın çift tamsayıya yuvarlamasına yol açabilir.Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the Round(Double) method may not appear to round midpoint values to the nearest even integer. Aşağıdaki örnekte, kayan nokta değerinin bir sonlu ikili temsili olmadığından, Round(Double) 11,5 değeriyle yönteme ilk çağrı 12 yerine 11 döndürür.In the following example, because the floating-point value .1 has no finite binary representation, the first call to the Round(Double) method with a value of 11.5 returns 11 instead of 12.

::: Code Language = "CSharp" Source = "~/Samples/Snippets/CSharp/VS_Snippets_CLR_System/System.Math.Round/CS/round2.cs" Interactive = "TRY-DotNet" ID = "Snippet1":::::: Code Language = "vb" kaynak = "~/Samples/Snippets/VisualBasic/VS_Snippets_CLR_System/System.Math.Round/vb/round2.vb" ID = "Snippet1"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.math.round/cs/round2.cs" interactive="try-dotnet" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.math.round/vb/round2.vb" id="Snippet1":::

Ayrıca bkz.

Şunlara uygulanır

Round(Decimal, Int32)

Ondalık değeri belirtilen sayıda kesirli basamağa yuvarlar ve orta nokta değerlerini en yakın çift sayıya yuvarlar.Rounds a decimal value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.

public:
 static System::Decimal Round(System::Decimal d, int decimals);
public static decimal Round (decimal d, int decimals);
static member Round : decimal * int -> decimal
Public Shared Function Round (d As Decimal, decimals As Integer) As Decimal

Parametreler

d
Decimal

Yuvarlanacak ondalık bir sayı.A decimal number to be rounded.

decimals
Int32

Dönüş değerindeki ondalık basamak sayısı.The number of decimal places in the return value.

Döndürülenler

Decimal

Öğesine d eşit sayıda kesirli basamak içeren en yakın sayı decimals .The number nearest to d that contains a number of fractional digits equal to decimals.

Özel durumlar

decimals 0 ' dan küçük veya 28 ' den büyük.decimals is less than 0 or greater than 28.

Sonuç bir ile aralığının dışındadır Decimal .The result is outside the range of a Decimal.

Açıklamalar

decimalsBağımsız değişkenin değeri 0 ile 28 arasında değişebilir.The value of the decimals argument can range from 0 to 28.

Bu yöntem, varsayılan yuvarlama kuralını kullanır MidpointRounding.ToEven .This method uses the default rounding convention of MidpointRounding.ToEven. Orta nokta değerleriyle sayıları yuvarlama hakkında bilgi için bkz. orta nokta değerleri ve yuvarlama kuralları .See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

ÖrnekExample

Aşağıdaki örnek, ondalık değerleri, tek kesirli basamak içeren değerlere iki kesirli basamakla yuvarlar.The following example rounds decimal values with two fractional digits to values that have a single fractional digit.

Console.WriteLine(Math.Round(3.44m, 1));
Console.WriteLine(Math.Round(3.45m, 1));
Console.WriteLine(Math.Round(3.46m, 1));
Console.WriteLine();

Console.WriteLine(Math.Round(4.34m, 1));
Console.WriteLine(Math.Round(4.35m, 1));
Console.WriteLine(Math.Round(4.36m, 1));
// The example displays the following output:
//       3.4
//       3.4
//       3.5
//
//       4.3
//       4.4
//       4.4
Public Module Example
   Sub Main()
      Console.WriteLine(Math.Round(3.44, 1)) 
      Console.WriteLine(Math.Round(3.45, 1)) 
      Console.WriteLine(Math.Round(3.46, 1)) 
      Console.WriteLine()
      
      Console.WriteLine(Math.Round(4.34, 1)) 
      Console.WriteLine(Math.Round(4.35, 1)) 
      Console.WriteLine(Math.Round(4.36, 1)) 
   End Sub  
End Module
' The example displays the following output:
'       3.4
'       3.4
'       3.5
'       
'       4.3
'       4.4
'       4.4

Ayrıca bkz.

Şunlara uygulanır

Round(Decimal)

Ondalık değeri en yakın tamsayı değerine yuvarlar ve orta nokta değerlerini en yakın çift sayıya yuvarlar.Rounds a decimal value to the nearest integral value, and rounds midpoint values to the nearest even number.

public:
 static System::Decimal Round(System::Decimal d);
public static decimal Round (decimal d);
static member Round : decimal -> decimal
Public Shared Function Round (d As Decimal) As Decimal

Parametreler

d
Decimal

Yuvarlanacak ondalık bir sayı.A decimal number to be rounded.

Döndürülenler

Decimal

Parametreye en yakın tamsayı d .The integer nearest the d parameter. ' Un kesirli bileşeni d iki tamsayı arasında, biri çift ve diğeri de iki tamsaysa, çift sayı döndürülür.If the fractional component of d is halfway between two integers, one of which is even and the other odd, the even number is returned. Bu yöntemin bir integral türü yerine bir döndürdüğünü unutmayın Decimal .Note that this method returns a Decimal instead of an integral type.

Özel durumlar

Sonuç bir ile aralığının dışındadır Decimal .The result is outside the range of a Decimal.

Örnekler

Aşağıdaki örnek Round(Decimal) yöntemini gösterir.The following example demonstrates the Round(Decimal) method. DecimalBu aşırı yükleme varsayılan kuralı kullandığından, 4,5 değerinin 5 yerine 4 ' e yuvardığına unutmayın ToEven .Note that the Decimal value of 4.5 rounds to 4 rather than 5, because this overload uses the default ToEven convention.

for (decimal value = 4.2m; value <= 4.8m; value+=.1m )
   Console.WriteLine("{0} --> {1}", value, Math.Round(value));
// The example displays the following output:
//       4.2 --> 4
//       4.3 --> 4
//       4.4 --> 4
//       4.5 --> 4
//       4.6 --> 5
//       4.7 --> 5
//       4.8 --> 5
Module Example
   Public Sub Main()
      For value As Decimal = 4.2d To 4.8d Step .1d
         Console.WriteLine("{0} --> {1}", value, Math.Round(value))
      Next   
   End Sub                                                                 
End Module
' The example displays the following output:
'       4.2 --> 4
'       4.3 --> 4
'       4.4 --> 4
'       4.5 --> 4
'       4.6 --> 5
'       4.7 --> 5
'       4.8 --> 5

Açıklamalar

Bu yöntem, varsayılan yuvarlama kuralını kullanır MidpointRounding.ToEven .This method uses the default rounding convention of MidpointRounding.ToEven. Orta nokta değerleriyle sayıları yuvarlama hakkında bilgi için bkz. orta nokta değerleri ve yuvarlama kuralları .See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

Ayrıca bkz.

Şunlara uygulanır

Round(Decimal, MidpointRounding)

Ondalık değeri en yakın tamsayıya yuvarlar ve orta nokta değerleri için belirtilen yuvarlama kuralını kullanır.Rounds a decimal value to the nearest integer, and uses the specified rounding convention for midpoint values.

public:
 static System::Decimal Round(System::Decimal d, MidpointRounding mode);
public static decimal Round (decimal d, MidpointRounding mode);
static member Round : decimal * MidpointRounding -> decimal
Public Shared Function Round (d As Decimal, mode As MidpointRounding) As Decimal

Parametreler

d
Decimal

Yuvarlanacak ondalık bir sayı.A decimal number to be rounded.

mode
MidpointRounding

dDiğer iki sayı arasında bir Midway olursa yuvarlama belirtimi.Specification for how to round d if it is midway between two other numbers.

Döndürülenler

Decimal

En yakın tamsayı d .The integer nearest d. dİki sayı arasında, biri çift ve diğeri de olan iki sayısa, bunlardan mode hangisinin döndürüleceğini belirler.If d is halfway between two numbers, one of which is even and the other odd, then mode determines which of the two is returned. Bu yöntemin bir integral türü yerine bir döndürdüğünü unutmayın Decimal .Note that this method returns a Decimal instead of an integral type.

Özel durumlar

mode geçerli bir değeri değil MidpointRounding .mode is not a valid value of MidpointRounding.

Sonuç bir ile aralığının dışındadır Decimal .The result is outside the range of a Decimal.

Açıklamalar

Orta nokta değerleriyle sayıları yuvarlama hakkında bilgi için bkz. orta nokta değerleri ve yuvarlama kuralları .See Midpoint values and rounding conventions for information about rounding numbers with midpoint values.

Önemli

Orta nokta değerlerini yuvarlarken, yuvarlama algoritması bir eşitlik testi gerçekleştirir.When rounding midpoint values, the rounding algorithm performs an equality test. Kayan nokta biçiminde ikili gösterimin ve Duyarlığın sorunları nedeniyle, yöntemin döndürdüğü değer beklenmedik olabilir.Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. Daha fazla bilgi için bkz. "yuvarlama ve duyarlık".For more information, see "Rounding and precision".

ÖrnekExample

Aşağıdaki örnek, yöntemi tarafından döndürülen değerleri Round(Decimal) , Round(Decimal, MidpointRounding) mode bağımsız değişkenine sahip yöntemi ToEven ve Round(Decimal, MidpointRounding) mode bağımsız değişkenini içeren AwayFromZero yöntemi gösterir.The following example displays values returned by the Round(Decimal) method, the Round(Decimal, MidpointRounding) method with a mode argument of ToEven, and the Round(Decimal, MidpointRounding) method with a mode argument of AwayFromZero.

Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default",
                  "ToEven", "AwayFromZero");
for (decimal value = 12.0m; value <= 13.0m; value += 0.1m)
   Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}",
                     value, Math.Round(value),
                     Math.Round(value, MidpointRounding.ToEven),
                     Math.Round(value, MidpointRounding.AwayFromZero));
// The example displays the following output:
//       Value      Default    ToEven     AwayFromZero
//       12.0       12         12         12
//       12.1       12         12         12
//       12.2       12         12         12
//       12.3       12         12         12
//       12.4       12         12         12
//       12.5       12         12         13
//       12.6       13         13         13
//       12.7       13         13         13
//       12.8       13         13         13
//       12.9       13         13         13
//       13.0       13         13         13
Module Example
   Public Sub Main()
      Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default", 
                        "ToEven", "AwayFromZero")
      For value As Decimal = 12.0d To 13.0d Step .1d
         Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}",
                           value, Math.Round(value), 
                           Math.Round(value, MidpointRounding.ToEven),
                           Math.Round(value, MidpointRounding.AwayFromZero))
      Next
   End Sub
End Module
' The example displays the following output:
'       Value      Default    ToEven     AwayFromZero
'       12         12         12         12
'       12.1       12         12         12
'       12.2       12         12         12
'       12.3       12         12         12
'       12.4       12         12         12
'       12.5       12         12         13
'       12.6       13         13         13
'       12.7       13         13         13
'       12.8       13         13         13
'       12.9       13         13         13
'       13.0       13         13         13

Ayrıca bkz.

Şunlara uygulanır