# Math.Round 方法

## 重载

 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) 将双精度浮点值舍入到指定数量的小数位，并为中点值使用指定的舍入规则。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) 将双精度浮点值舍入到最接近的整数，并为中点值使用指定的舍入约定。Rounds a double-precision floating-point value to the nearest integer, and uses the specified rounding convention for midpoint values. 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) 将双精度浮点值舍入到最接近的整数值，并将中点值舍入到最接近的偶数。Rounds a double-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number. 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) 将小数值舍入到最接近的整数值，并将中点值舍入到最接近的偶数。Rounds a decimal value to the nearest integral value, and rounds midpoint values to the nearest even number. 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?

- 或 --or-

Round(Double)

- 或 --or-

Round(Double, MidpointRounding)

- 或 --or-

Round(Double, Int32)

- 或 --or-

Round(Double, 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 Standard 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 值数组的 true 平均值，然后使用两种约定来计算数组中的值的值。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. 在此示例中，如果舍入到最接近的是相同的，则为真正的平均值和结果。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 值的重载，则可以将 Double 更改为 Decimal 值，并改为调用舍入 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 方法包含接受 DecimalDouble类型的参数的重载。The Round method includes overloads that accept arguments of type Decimal and Double. 没有用于舍入类型 Single的值的方法。There 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. 如果将 Single 值16.325 传递到 Round 方法，并使用舍入到最接近的约定舍入为两位小数位数，则结果为16.33，而不是预期的16.32 结果。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)

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

decimals
Int32

mode
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.
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)
'
``````

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

digits
Int32

mode
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.object # 3][！ code-vb system.string# 3][!code-csharpSystem.Math.Round#3] [!code-vbSystem.Math.Round#3]

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

mode
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 system.string# 4][！ code-vb system.string# 4][!code-csharpSystem.Math.Round#4] [!code-vbSystem.Math.Round#4]

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

digits
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 system.string# 2][！ code-vb system.exception# 2][!code-csharpSystem.Math.Round#2] [!code-vbSystem.Math.Round#2]

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

### 注解

``````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
``````
``````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-csharp system.string# 1][！ code-vb system.exception# 1][!code-csharpSystem.Math.Round#1] [!code-vbSystem.Math.Round#1]

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

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

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

### 示例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)

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

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