在C#中, Math.Round()是Math类方法,用于将值舍入到最接近的整数或小数位数。此方法还有另一个重载,您可以使用该重载指定返回值中小数点后的位数。它返回数字的最接近的值,其精度等于传递的第二个参数。如果要舍入的值恰好介于偶数和奇数之间,则返回偶数。 Math.Round适用IEEE标准754,第4节。
可以通过更改传递的参数的数量和类型来重载此方法。 Math.Round()方法的重载列表中共有8种方法,其中C#中已经讨论了4种方法。 Math.Round()方法|设置– 1 。
- 圆数(双)
- Math.Round(Double,Int32)
- Math.Round(十进制)
- Math.Round(Decimal,Int32)
- Math.Round(Double,Int32,MidpointRounding)
- Math.Round(Double,MidpointRounding)
- Math.Round(Decimal,Int32,MidpointRounding)
- Math.Round(十进制,MidpointRounding)
Math.Round(Double,Int32,MidpointRounding)
此方法用于将双精度浮点值舍入为指定数量的小数位数。参数指定如果值在两个数字之间的中间值时如何舍入。
句法:
public static double Round (double val, int digits, MidpointRounding mode);
参数:
val: It is the required double-precision floating-point number which is to be rounded and type of this parameter is System.Double.
digits: It is the number of fractional digits in the return value and type of this parameter is System.Int32.
mode: Specification for how to round val if it is midway between two other numbers and works as MidpointRounding.
返回类型:此方法返回最接近val的数字,该数字的小数位数等于digits 。如果val的小数位数少于digits ,则val不变地返回。此方法的返回类型为System.Double 。
例外情况:
- ArgumentOutOfRangeException :如果数字小于0或大于15。
- ArgumentException :如果该模式不是MidpointRounding的有效值。
例子:
// C# program to demonstrate the
// Math.Round(Double, Int32,
// MidpointRounding) method
using System;
class Geeks
{
// Main Method
public static void Main()
{
// The 4 values are store in an double
// type array name 'val'
double[] val = {4.125, 4.135, 4.165, 4.175};
Console.WriteLine("Rounded values are:");
// 'foreach' loop iterates through
// each item from the array 'values'
// and storing the items in a new
// variable 'val'
foreach(double value in val)
// '{0}' specify the variable 'val' which is
// in 'foreach' loop and '{1}' specify the
// rounded value, here '2' defines the number
// of digit after point, e.g. 4.135 == 4.14,
// after '4.' there is 2 digits'.14'
// and here '.ToEven' select the nearest even
// number e.g 4.125 == 4.12, here nearest even
// number is '12',
Console.WriteLine("{0} == {1}", value, Math.Round(value, 2,
MidpointRounding.ToEven));
}
}
Rounded values are:
4.125 == 4.12
4.135 == 4.14
4.165 == 4.16
4.175 == 4.18
注意:在某些情况下,由于精度的损失,此方法可能无法对mode参数指定的中点值进行舍入,这可能是由于将十进制值表示为浮点数或对浮点值执行算术运算而导致的。在上面的示例中对此进行了说明,其中4.135舍入为4.13,而不是4.14。发生这种情况是因为该方法在内部将val乘以10位,并且在这种情况下,乘法运算会损失精度。
Math.Round(Double,MidpointRounding)
此方法用于将双精度浮点值四舍五入到最接近的整数。参数指定如果值在两个数字之间的中间值时如何舍入。
句法:
public static double Round (double val, MidpointRounding mode);
参数:
val: It is the required double-precision floating-point number which is to be rounded and type of this parameter is System.Double.
mode: Specification for how to round val if it is midway between two other numbers and works as MidpointRounding.
返回类型:此方法返回整数最接近的值。如果val位于两个整数之间的中间,其中一个为偶数,另一个为奇数,则mode确定返回两个整数中的哪一个。此方法的返回类型为System.Double 。
异常:如果该模式不是MidpointRounding的有效值,则此方法提供ArgumentException。
例子:
// C# program to demonstrate the
// Math.Round(Double, MidpointRounding) method
using System;
class Geeks
{
// Main Method
public static void Main()
{
//'val' is double type variable
// which holds the value 4.1
double val = 4.1;
Console.WriteLine("Inside Loop:\n");
//'for loop', it execute the next
// output for 8 times
for (int i = 0; i <= 8; i++)
{
// '{0}' specify the variable 'val' and
// '{1}' specify the rounded value
Console.WriteLine("{0} = {1}", val, Math.Round(val,
MidpointRounding.AwayFromZero));
// increment 'val' by '0.1'
val += 0.1;
}
// a new value is assigned
// to variable 'val'
val = 4.5;
// prints a new line
Console.WriteLine();
//'{0}'specify the variable 'val' in which a new
// value 4.5 is assigned and '{1}' specify the
// new rounded value
Console.WriteLine("Outside Loop : {0} = {1}", val,
Math.Round(val, MidpointRounding.AwayFromZero));
}
}
Inside Loop:
4.1 = 4
4.2 = 4
4.3 = 4
4.4 = 4
4.5 = 4
4.6 = 5
4.7 = 5
4.8 = 5
4.9 = 5
Outside Loop : 4.5 = 5
注意:在某些情况下,由于精度损失,这种方法可能无法将中点值舍入到最接近的偶数整数,这可能是由于将十进制值表示为浮点数或对浮点值执行算术运算而导致的。在上面的示例中,由于浮点值0.1没有有限的二进制表示形式,因此第一次调用值为4.5的方法将返回4而不是5。
Math.Round(Decimal,Int32,MidpointRounding)
此方法用于将十进制值舍入为指定数量的小数位数。参数指定如果值在两个数字之间的中间值时如何舍入。
句法:
public static decimal Round (decimal val, int num, MidpointRounding mode);
参数:
val: It is the required decimal number of type System.Decimal which is to be rounded.
num: It specifies the number of decimal places in the return value and type of this parameter is System.Int32.
mode: It provides specification for how to round val if it is midway between two other numbers.
返回类型:最接近val的数字,其中包含等于num的小数位数。如果val的小数位数少于小数位数,则val不变地返回。
例外情况:
- ArgumentOutOfRangeException :如果num小于0或大于28。
- ArgumentException :如果该模式不是MidpointRounding的有效值。
- OverflowException :如果结果超出Decimal的范围。
例子:
// C# program to demonstrate the
// Math.Round(Decimal, Int32,
// MidpointRounding) method
using System;
class Geeks
{
// Main Method
public static void Main()
{
// The 6 values are store in an
// double type array name val,
double[] val = {2.275, 2.375, 2.455,
3.525, 3.635, 3.465 };
// prints 'Rounded values :'
Console.WriteLine("Rounded Values: ");
//'foreach' loop iterates through each item
// from the array 'val' and storing the items
// in a new variable 'value'
foreach(double value in val)
// '{0}' specify the variable 'value'and
// '{1}' specify the rounded value,
Console.WriteLine("{0} == {1}", value, Math.Round(value,
2, MidpointRounding.ToEven));
}
}
Rounded Values:
2.275 == 2.28
2.375 == 2.38
2.455 == 2.46
3.525 == 3.52
3.635 == 3.64
3.465 == 3.46
Math.Round(十进制,MidpointRounding)
此方法用于将十进制值舍入到最接近的整数。参数指定如果值在两个数字之间的中间值时如何舍入。
句法:
public static decimal Round (decimal val, MidpointRounding mode);
参数:
val: It is the required decimal number of type System.Decimal which is to be rounded.
mode: It provides specification for how to round val if it is midway between two other numbers.
返回类型:返回最接近val的整数。如果val位于两个数字的中间,其中一个为偶数,另一个为奇数,则mode确定返回两个中的哪一个。
例外情况:
- ArgumentException :如果该模式不是MidpointRounding的有效值。
- OverflowException :如果结果超出Decimal的范围。
例子:
// C# program to demonstrate the
// Math.Round(Decimal, MidpointRounding) method
using System;
class Geeks
{
// Main Method
public static void Main()
{
// The 6 values are store in a
// double type array name val
double[] val = {2.275, 2.375, 2.455,
3.525, 3.635, 3.465 };
// prints 'Rounded values :'
Console.WriteLine("Rounded values :");
//'foreach' loop iterates through each item
// from the array 'val' and storing the items
// in a new variable 'value'
foreach(double value in val)
// '{0}' specify the variable 'value' and
// '{1}' specify the rounded value
Console.WriteLine("{0} == {1}", value, Math.Round(value,
MidpointRounding.ToEven));
}
}
Rounded values :
2.275 == 2
2.375 == 2
2.455 == 2
3.525 == 4
3.635 == 4
3.465 == 3
参考: https://docs.microsoft.com/zh-cn/dotnet/api/system.math.round?view=netframework-4.7.2