在C#中, 数学第(轮) 是一种数学类方法,用于将值舍入到最接近的整数或特定的小数位数。此方法还有另一个重载,通过该重载,可以指定返回值中小数点以外的位数。它返回最接近的数值,精度等于传递的第二个参数。如果要舍入的值正好介于偶数和奇数之间,则返回偶数。 数学圆形的 适用IEEE标准754第4节。
通过更改传递的参数的数量和类型,可以重载此方法。该方法的重载列表中总共有8种方法 数学第(轮) 方法 4. 已经讨论过了 C#|数学。Round()方法| Set–1 .
- 数学圆形(双人)
- 数学圆形(双倍,Int32)
- 数学四舍五入(十进制)
- 数学四舍五入(十进制,Int32)
- 数学四舍五入(双精度、Int32、中点四舍五入)
- 数学圆形(双圆形,中点圆形)
- 数学四舍五入(十进制、Int32、中点四舍五入)
- 数学四舍五入(十进制、中点四舍五入)
数学四舍五入(双精度、Int32、中点四舍五入)
此方法用于将双精度浮点值舍入到指定的小数位数。参数指定如果值介于两个数字之间,如何对其进行四舍五入。
语法:
public static double Round (double val, int digits, MidpointRounding mode);
参数:
瓦尔 :需要四舍五入的双精度浮点数,此参数的类型为 系统双重的 .
数字 :返回值中的小数位数,此参数的类型为 系统Int32 .
模式 :如何取整的规范 瓦尔 如果它位于其他两个数字的中间,并且作为 中点舍入 .
返回类型: 此方法返回最接近的数字 瓦尔 其小数位数等于 数字 .如果 瓦尔 小数位数少于 数字 ,则val将原封不动地返回。此方法的返回类型为 系统双重的 .
例外情况:
- ArgumentOutOfRange异常 :如果 数字 小于0或大于15。
- 辩论例外 :如果 模式 不是有效的中点舍入值。
例子:
// 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
注: 在某些情况下,此方法可能不会按照 模式 由于将十进制值表示为浮点数或对浮点值执行算术运算而导致的精度损失而导致的参数。上述示例对此进行了说明,其中4.135四舍五入为4.13,而不是4.14。发生这种情况是因为该方法在内部相乘 瓦尔 10点之前 数字 ,在这种情况下,乘法运算的精度会降低。
数学圆形(双圆形,中点圆形)
此方法用于将双精度浮点值舍入为最接近的整数。参数指定如果值介于两个数字之间,如何对其进行四舍五入。
语法:
public static double Round (double val, MidpointRounding mode);
参数:
瓦尔 :需要四舍五入的双精度浮点数,此参数的类型为 系统双重的 .
模式 :如何取整的规范 瓦尔 如果它位于其他两个数字的中间,并且作为 中点舍入 .
返回类型: 此方法返回最接近的整数值。如果 瓦尔 是两个整数的中间值,其中一个是偶数,另一个是奇数,然后mode决定返回这两个整数中的哪一个。此方法的返回类型为 系统双重的 .
例外情况: 这种方法给出 辩论例外 如果 模式 不是有效的中点舍入值。
例子:
// 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:" ); //'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。
数学四舍五入(十进制、Int32、中点四舍五入)
此方法用于将十进制值四舍五入到指定的小数位数。参数指定如果值介于两个数字之间,如何对其进行四舍五入。
语法:
public static decimal Round (decimal val, int num, MidpointRounding mode);
参数:
瓦尔: 它是类型所需的十进制数 系统十进制的 要四舍五入。 号码: 它指定返回值中的小数位数,该参数的类型为 系统Int32 . 模式: 它提供了如何取整的规范 瓦尔 如果它在另外两个数字中间。
返回类型: 最接近的号码 瓦尔 它包含的小数位数等于 号码 如果 瓦尔 小数位数比小数位数少, 瓦尔 返回时保持不变。
例外情况:
- ArgumentOutOfRange异常 :如果 号码 小于0或大于28。
- 辩论例外 :如果 模式 不是有效的中点舍入值。
- 溢出异常 :如果结果超出a的范围 十进制的 .
例子:
// 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
数学四舍五入(十进制、中点四舍五入)
此方法用于将十进制值四舍五入到最接近的整数。参数指定如果值介于两个数字之间,如何对其进行四舍五入。
语法:
public static decimal Round (decimal val, MidpointRounding mode);
参数:
瓦尔: 它是类型所需的十进制数 系统十进制的 要四舍五入。 模式: 它提供了如何取整的规范 瓦尔 如果它在另外两个数字中间。
返回类型: 它返回最接近的整数 瓦尔 如果 瓦尔 介于两个数字之间,一个是偶数,另一个是奇数 模式 确定返回哪一个。
例外情况:
- 辩论例外 :如果 模式 不是有效的中点舍入值。
- 溢出异常 :如果结果超出a的范围 十进制的 .
例子:
// 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/en-us/dotnet/api/system.math.round?view=netframework-4.7.2