在C#中, MathF.Round()是MathF类方法,用于将值四舍五入到最接近的整数或小数位数。它返回数字的最接近的值,其精度等于传递的第二个参数。如果要舍入的值恰好介于偶数和奇数之间,则返回偶数。 MathF.Round适用IEEE标准754,第4节。可以通过更改传递的参数的数量和类型来重载此方法。 MathF.Round()方法的重载列表中有4种方法,如下所示:
- MathF.Round(Single)方法
- MathF.Round(Single,Int32)方法
- MathF.Round(Single,Int32,MidpointRounding)方法
- MathF.Round(Single,MidpointRounding)方法
在这里,我们将讨论最后两种方法。
Math.Round(Single,Int32,MidpointRounding)方法
此方法用于将单个精度浮点值四舍五入为指定数量的小数位数。参数指定如果值在两个数字之间的中间值时如何舍入。
Syntax: public static float Round (float x, int digits, MidpointRounding mode);
Parameters:
x: It is the required single-precision floating-point number which is to be rounded and type of this parameter is System.Single.
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 x if it is midway between two other numbers and works as MidpointRounding.
返回类型:此方法返回最接近x的数字,该数字的小数位数等于digits 。如果x的小数位数少于digits ,则x不变返回。此方法的返回类型为System.Single 。
例外情况:
- ArgumentOutOfRangeException :如果数字小于0或大于15。
- ArgumentException :如果该模式不是MidpointRounding的有效值。
例子:
// C# program to demonstrate the
// MathF.Round(Single, Int32,
// MidpointRounding) method
using System;
class Geeks {
// Main Method
public static void Main()
{
// The 4 values are store in an float
// type array name 'val'
float[] x = { 8.115f, 7.135f, 9.165f, 6.175f };
Console.WriteLine("Rounded values are:");
// 'foreach' loop iterates through
// each item from the array 'values'
// and storing the items in a new
// variable 'x'
foreach(float value in x)
// '{0}' specify the variable 'x' which is
// in 'foreach' loop and '{1}' specify the
// rounded value, here '2' defines the number
// of digit after point, e.g. 4.135f == 4.14f,
// after '4f.' there is 2 digits'.14f'
// and here '.ToEven' select the nearest even
// number e.g 4.125f == 4.12f, here nearest even
// number is '12f',
Console.WriteLine("{0} == {1}", value, MathF.Round(value, 2,
MidpointRounding.ToEven));
}
}
Rounded values are:
8.115 == 8.12
7.135 == 7.14
9.165 == 9.16
6.175 == 6.18
MathF.Round(Single,MidpointRounding)方法
此方法用于将单个精度浮点值舍入到最接近的整数。参数指定如果值在两个数字之间的中间值时如何舍入。
Syntax: public static float Round (float x, MidpointRounding mode);
Parameters:
x: It is the required single-precision floating-point number which is to be rounded and type of this parameter is System.Single.
mode: Specification for how to round x if it is midway between two other numbers and works as MidpointRounding.
返回类型:此方法返回整数最接近的值。如果x位于两个整数之间的中间,其中一个为偶数,另一个为奇数,则mode确定返回两个中的哪一个。此方法的返回类型为System.Single 。
异常:如果该模式不是MidpointRounding的有效值,则此方法提供ArgumentException。
例子:
// C# program to demonstrate the
// MathF.Round(Single, MidpointRounding)
// method
using System;
class Geeks {
// Main Method
public static void Main()
{
// 'x' is float type variable
// which holds the value 7.1f
float x = 7.1f;
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 'x' and
// '{1}' specify the rounded value
Console.WriteLine("{0} = {1}", x, MathF.Round(x,
MidpointRounding.AwayFromZero));
// increment 'x' by '0.1'
x += 0.1f;
}
// a new value is assigned
// to variable 'x'
x = 4.5f;
// prints a new line
Console.WriteLine();
//'{0}'specify the variable 'x' in which a new
// value 4.5f is assigned and '{1}' specify the
// new rounded value
Console.WriteLine("Outside Loop : {0} = {1}", x,
MathF.Round(x, MidpointRounding.AwayFromZero));
}
}
Inside Loop:
7.1 = 7
7.2 = 7
7.3 = 7
7.4 = 7
7.5 = 7
7.599999 = 8
7.699999 = 8
7.799999 = 8
7.899999 = 8
Outside Loop : 4.5 = 5