C# 中的 Math.Round() 方法
C# 中的 Math.Round() 方法将一个值舍入到最接近的整数或指定的小数位数。
方法
以下是 Math.Round() 重载的方法 -
Math.Round(Double) Math.Round(Double, Int32) Math.Round(Double, Int32, MidpointRounding) Math.Round(Double, MidpointRounding) Math.Round(Decimal) Math.Round(Decimal, Int32) Math.Round(Decimal, Int32, MidpointRounding) Math.Round(Decimal, MidpointRounding)
示例
让我们来看一个实现 Math.Round() 方法的示例,即 Math.Round(十进制) -
using System; public class Demo { public static void Main(){ Decimal val1 = 78.12m; Decimal val2 = 30.675m; Console.WriteLine("Decimal Value = " + val1); Console.WriteLine("Rounded value = " + Math.Round(val1)); Console.WriteLine("Decimal Value = " + val2); Console.WriteLine("Rounded value = " + Math.Round(val2)); } }
输出
这将产生以下输出 -
Decimal Value = 78.12 Rounded value = 78 Decimal Value = 30.675 Rounded value = 31
示例
让我们来看另一个实现 Math.Round() 方法的示例,即 Math.Round(双精度) -
using System; public class Demo { public static void Main(){ Double val1 = 23.10; Double val2 = 90.98; Console.WriteLine("Double Value = " + val1); Console.WriteLine("Rounded value = " + Math.Round(val1)); Console.WriteLine("Double Value = " + val2); Console.WriteLine("Rounded value = " + Math.Round(val2)); } }
输出
这将产生以下输出 -
Double Value = 23.1 Rounded value = 23 Double Value = 90.98 Rounded value = 91
广告