# Math.RoundMath.RoundMath.RoundMath.Round Method

## 多載

 Round(Decimal, Int32, MidpointRounding) Round(Decimal, Int32, MidpointRounding) Round(Decimal, Int32, MidpointRounding) Round(Decimal, Int32, MidpointRounding) 將十進位值四捨五入到小數數字的指定數字，並使用中間點值的指定進位慣例。Rounds a decimal value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values. Round(Double, Int32, MidpointRounding) Round(Double, Int32, MidpointRounding) Round(Double, Int32, MidpointRounding) Round(Double, Int32, MidpointRounding) 將雙精確度浮點數值四捨五入到小數數字的指定數字，並使用中間點值的指定進位慣例。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) Round(Double, MidpointRounding) Round(Double, MidpointRounding) Round(Double, MidpointRounding) 將雙精確度浮點數值四捨五入到最接近的整數，並使用中間點值的指定進位慣例。Rounds a double-precision floating-point value to the nearest integer, and uses the specified rounding convention for midpoint values. Round(Double, Int32) Round(Double, Int32) Round(Double, Int32) Round(Double, Int32) 將雙精確度浮點數值四捨五入到小數數字的指定數字，並將中間點值四捨五入到最接近的偶數。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) Round(Double) Round(Double) Round(Double) 將雙精確度浮點數值四捨五入到最接近的整數值，並將中間點值四捨五入到最接近的偶數。Rounds a double-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number. Round(Decimal, Int32) Round(Decimal, Int32) Round(Decimal, Int32) Round(Decimal, Int32) 將十進位值四捨五入到小數數字的指定數字，並將中間點值四捨五入到最接近的偶數。Rounds a decimal value to a specified number of fractional digits, and rounds midpoint values to the nearest even number. Round(Decimal) Round(Decimal) Round(Decimal) Round(Decimal) 將十進位值四捨五入到最接近的整數值，並將中間點值四捨五入到最接近的偶數。Rounds a decimal value to the nearest integral value, and rounds midpoint values to the nearest even number. Round(Decimal, MidpointRounding) Round(Decimal, MidpointRounding) Round(Decimal, MidpointRounding) Round(Decimal, MidpointRounding) 將十進位值四捨五入到最接近的整數，並使用中間點值的指定進位慣例。Rounds a decimal value to the nearest integer, and uses the specified rounding convention for midpoint values.

## 備註

#### 我呼叫的方法？Which method do I call?

To CallCall

-或--or-

Round(Double)

-或--or-

Round(Double, MidpointRounding)

-或--or-

Round(Double, Int32)

-或--or-

Round(Double, Int32, MidpointRounding)
RoundSingle藉由指定捨入慣例和最小化遺失有效位數的小數位數的指定數目的值。Round a Single value to a specified number of fractional digits by using a specified rounding convention and minimizing the loss of precision. Convert the Single to a Decimal and call Round(Decimal, Int32, MidpointRounding).

#### 中間值和捨入的慣例Midpoint values and rounding conventions

Round方法支援兩種捨入的慣例來處理中點值：The Round method supports two rounding conventions for handling midpoint values:

• 遠離零四捨五入Rounding away from zero

中間值會捨入為下一步 的數字背離零。Midpoint values are rounded to the next number away from zero. 比方說，3.75 捨入為 3.8，3.85 的會四捨五入為 3.9，-3.75 捨入為-3.8，和-3.85 會捨入為-3.9。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. 這種形式的捨入由MidpointRounding.AwayFromZero列舉成員。This form of rounding is represented by the MidpointRounding.AwayFromZero enumeration member.

遠離零四捨五入為最廣泛的已知的形式的四捨五入。Rounding away from zero is the most widely known form of rounding.

• 四捨五入為最接近，或成雙Rounding to nearest, or banker's rounding

中間值會四捨五入至最接近的偶數。Midpoint values are rounded to the nearest even number. 比方說，3.75 和 3.85 捨入到 3.8，，和-3.75 和-3.85 捨入-3.8。For example, both 3.75 and 3.85 round to 3.8, and both -3.75 and -3.85 round to -3.8. 這種形式的捨入由MidpointRounding.ToEven列舉成員。This form of rounding is represented by the MidpointRounding.ToEven enumeration member.

四捨五入為最接近是標準形式的捨入財務和統計作業中使用。Rounding to nearest is the standard form of rounding used in financial and statistical operations. 它符合 IEEE 標準 754，第 4 節。It conforms to IEEE Standard 754, section 4. 多個的捨入運算中使用時，它會減少因一致四捨五入單向中的點值捨入錯誤。When used in multiple rounding operations, it reduces the rounding error that is caused by consistently rounding midpoint values in a single direction. 在某些情況下，這種捨入錯誤很可觀。In some cases, this rounding error can be significant.

下列範例說明可能會造成一致四捨五入單向中的點值的偏差。The following example illustrates the bias that can result from consistently rounding midpoint values in a single direction. 範例會計算陣列的真正平均值Decimal值，然後再計算平均值，當陣列中的值會捨入所使用的兩個的慣例。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. 在此範例中，則為 true 的平均和結果時捨入為最接近的平均值都相同。In this example, the true mean and the mean that results when rounding to nearest are the same. 不過，遠離零四捨五入時結果的平均值不同.05 （或 3.6%）從真正的平均值。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
``````

Round(Decimal) ToEven
Round(Double) ToEven
Round(Decimal, Int32) ToEven
Round(Double, Int32) ToEven
Round(Decimal, MidpointRounding) 取決於`mode`參數。Determined by `mode` parameter.
Round(Double, MidpointRounding) 取決於`mode`參數Determined by `mode` parameter
Round(Decimal, Int32, MidpointRounding) 取決於`mode`參數Determined by `mode` parameter
Round(Double, Int32, MidpointRounding) 取決於`mode`參數Determined by `mode` parameter

### 捨入和有效位數Rounding and precision

``````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++)

Console.WriteLine();

value = 11.5;
}

{
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
Next
Console.WriteLine()

value = 11.5
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
``````

• 當小數的值無法以精確的浮點類型的二進位格式。When a fractional value cannot be expressed precisely in the floating-point type's binary format.

• 當要四捨五入的值會計算從一個或多個浮點運算。When the value to be rounded is calculated from one or more floating-point operations.

• 數值捨入何時Single而非DoubleDecimalWhen the value to be rounded is a Single rather than a Double or Decimal. 如需詳細資訊，請參閱下一步 區段中，捨入和單精確度浮點值For more information, see the next section, Rounding and single-precision floating-point values.

• 如果四捨五入運算呼叫多載會無條件Double值，您可以變更DoubleDecimal值，並呼叫將捨入的多載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. 雖然Decimal資料型別也有表示法的問題和遺失有效位數，這些問題不遠較不常見。Although the Decimal data type also has problems of representation and loss of precision, these issues are far less common.

• 定義自訂的捨入演算法會執行 「 幾乎等於"的測試，以判斷要四捨五入的值是否接受接近中間值。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. 下列範例會定義`RoundApproximate`檢查小數值是否為夠靠近受制於捨入中間點的中點值的方法。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. 如範例輸出所示，它會修正前一範例所示的捨入問題。As the output from the example shows, it corrects the rounding problem shown in the previous example.

``````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++)

Console.WriteLine();

value = 11.5;
}

{
const double tolerance = 8e-14;

Console.WriteLine("{0,5:N1} {0,20:R}  {1,12} {2,15}",
value,
RoundApproximate(value, 0, tolerance, MidpointRounding.ToEven),
RoundApproximate(value, 0, tolerance, MidpointRounding.AwayFromZero));
return value + .1;
}

private static double RoundApproximate(double dbl, int digits, double margin,
MidpointRounding mode)
{
double fraction = dbl * Math.Pow(10, digits);
double value = Math.Truncate(fraction);
fraction = fraction - value;
if (fraction == 0)
return dbl;

double tolerance = margin * dbl;
// Determine whether this is a midpoint value.
if ((fraction >= .5 - tolerance) & (fraction <= .5 + tolerance)) {
if (mode == MidpointRounding.AwayFromZero)
return (value + 1)/Math.Pow(10, digits);
else
if (value % 2 != 0)
return (value + 1)/Math.Pow(10, digits);
else
return value/Math.Pow(10, digits);
}
// Any remaining fractional value greater than .5 is not a midpoint value.
if (fraction > .5)
return (value + 1)/Math.Pow(10, digits);
else
return value/Math.Pow(10, digits);
}
}
// 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            12              12
//        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}\n",
"Value", "Full Precision", "ToEven",
"AwayFromZero")
For ctr As Integer = 0 To 5
Next
Console.WriteLine()

value = 11.5
End Sub

Private Function RoundValueAndAdd(value As Double) As Double
Const tolerance As Double = 8e-14
Console.WriteLine("{0,5:N1} {0,20:R}  {1,12} {2,15}",
value,
RoundApproximate(value, 0, tolerance, MidpointRounding.ToEven),
RoundApproximate(value, 0, tolerance, MidpointRounding.AwayFromZero))
Return value + .1
End Function

Private Function RoundApproximate(dbl As Double, digits As Integer, margin As Double,
mode As MidpointRounding) As Double
Dim fraction As Double = dbl * Math.Pow(10, digits)
Dim value As Double = Math.Truncate(fraction)
fraction = fraction - value
If fraction = 0 Then Return dbl

Dim tolerance As Double = margin * dbl
' Determine whether this is a midpoint value.
If (fraction >= .5 - tolerance) And (fraction <= .5 + tolerance) Then
If mode = MidpointRounding.AwayFromZero Then
Return (value + 1)/Math.Pow(10, digits)
Else
If value Mod 2 <> 0 Then
Return (value + 1)/Math.Pow(10, digits)
Else
Return value/Math.Pow(10, digits)
End If
End If
End If
' Any remaining fractional value greater than .5 is not a midpoint value.
If fraction > .5 Then
Return (value + 1)/Math.Pow(10, digits)
Else
return value/Math.Pow(10, digits)
End If
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            12              12
'        11.6   11.599999999999998            12              12
'
'        11.5                 11.5            12              12
``````

#### 四捨五入和單精確度浮點值Rounding and single-precision floating-point values

Round方法包含接受類型引數的多載DecimalDoubleThe Round method includes overloads that accept arguments of type Decimal and Double. 捨入值的型別沒有方法SingleThere are no methods that round values of type Single. 如果您傳遞Single值設為其中的多載Round方法，它是轉型 （在 C# 中)，或 （在 Visual Basic) 轉換成Double，和對應Round多載Double參數稱為。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. 雖然這是擴展轉換，但它通常牽涉到遺失有效位數，如下列範例所示。Although this is a widening conversion, it often involves a loss of precision, as the following example illustrates. Single16.325 的值會傳遞至Round方法和捨入成兩個小數位數，使用四捨五入至最接近的慣例，結果是 16.32 16.33 和未預期結果。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
``````

##

## Round(Decimal, Int32, MidpointRounding)Round(Decimal, Int32, MidpointRounding)Round(Decimal, Int32, MidpointRounding)Round(Decimal, Int32, MidpointRounding)

``````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``

#### 參數

d
Decimal Decimal Decimal Decimal

decimals
Int32 Int32 Int32 Int32

mode
MidpointRounding MidpointRounding MidpointRounding MidpointRounding

#### 例外狀況

`decimals` 小於 0 或大於 28。`decimals` is less than 0 or greater than 28.

`mode` 不是有效的 MidpointRounding 值。`mode` is not a valid value of MidpointRounding.

### 備註

`decimals`引數的範圍可以從 0 到 28。The value of the `decimals` argument can range from 0 to 28.

### 範例Example

``````// 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 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)
'
``````

## Round(Double, Int32, MidpointRounding)Round(Double, Int32, MidpointRounding)Round(Double, Int32, MidpointRounding)Round(Double, Int32, MidpointRounding)

``````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``

#### 參數

value
Double Double Double Double

digits
Int32 Int32 Int32 Int32

mode
MidpointRounding MidpointRounding MidpointRounding MidpointRounding

#### 例外狀況

`digits` 小於 0 或大於 15。`digits` is less than 0 or greater than 15.

`mode` 不是有效的 MidpointRounding 值。`mode` is not a valid value of MidpointRounding.

### 備註

`digits`引數的範圍可以從 0 到 15 之間。The value of the `digits` argument can range from 0 to 15. 請注意，15 整數和小數的數字所支援的最大數目Double型別。Note that 15 is the maximum number of integral and fractional digits supported by the Double type.

### 範例Example

`````` 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)
``````

### 給呼叫者的注意事項

[!code-csharpSystem.Math.Round#3] [!code-vbSystem.Math.Round#3][!code-csharpSystem.Math.Round#3] [!code-vbSystem.Math.Round#3]

## Round(Double, MidpointRounding)Round(Double, MidpointRounding)Round(Double, MidpointRounding)Round(Double, MidpointRounding)

``````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``

#### 參數

value
Double Double Double Double

mode
MidpointRounding MidpointRounding MidpointRounding MidpointRounding

#### 例外狀況

`mode` 不是有效的 MidpointRounding 值。`mode` is not a valid value of MidpointRounding.

### 範例Example

``````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.0       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.0       13         13         13
``````

### 給呼叫者的注意事項

[!code-csharp-interactiveSystem.Math.Round#4] [!code-vbSystem.Math.Round#4][!code-csharp-interactiveSystem.Math.Round#4] [!code-vbSystem.Math.Round#4]

## Round(Double, Int32)Round(Double, Int32)Round(Double, Int32)Round(Double, Int32)

``````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``

#### 參數

value
Double Double Double Double

digits
Int32 Int32 Int32 Int32

#### 例外狀況

`digits` 小於 0 或大於 15。`digits` is less than 0 or greater than 15.

### 備註

`digits`引數的範圍可以從 0 到 15 之間。The value of the `digits` argument can range from 0 to 15. 請注意，15 整數和小數的數字所支援的最大數目Double型別。Note that 15 is the maximum number of integral and fractional digits supported by the Double type.

### 範例Example

``````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
``````

### 給呼叫者的注意事項

[!code-csharp-interactiveSystem.Math.Round#2] [!code-vbSystem.Math.Round#2][!code-csharp-interactiveSystem.Math.Round#2] [!code-vbSystem.Math.Round#2]

## Round(Double)Round(Double)Round(Double)Round(Double)

``````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``

#### 參數

a
Double Double Double Double

### 備註

``````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
``````

### 範例Example

``````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
``````
``````Imports System

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
``````

### 給呼叫者的注意事項

[!code-csharpSystem.Math.Round#1] [!code-vbSystem.Math.Round#1][!code-csharpSystem.Math.Round#1] [!code-vbSystem.Math.Round#1]

## Round(Decimal, Int32)Round(Decimal, Int32)Round(Decimal, Int32)Round(Decimal, Int32)

``````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``

#### 參數

d
Decimal Decimal Decimal Decimal

decimals
Int32 Int32 Int32 Int32

#### 例外狀況

`decimals` 小於 0 或大於 28。`decimals` is less than 0 or greater than 28.

### 備註

`decimals`引數的範圍可以從 0 到 28。The value of the `decimals` argument can range from 0 to 28.

### 範例Example

``````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
``````

## Round(Decimal)Round(Decimal)Round(Decimal)Round(Decimal)

``````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``

#### 參數

d
Decimal Decimal Decimal Decimal

### 範例Example

``````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
``````

## Round(Decimal, MidpointRounding)Round(Decimal, MidpointRounding)Round(Decimal, MidpointRounding)Round(Decimal, MidpointRounding)

``````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``

#### 參數

d
Decimal Decimal Decimal Decimal

mode
MidpointRounding MidpointRounding MidpointRounding MidpointRounding

#### 例外狀況

`mode` 不是有效的 MidpointRounding 值。`mode` is not a valid value of MidpointRounding.

### 範例Example

``````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         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
``````