📅  最后修改于: 2023-12-03 15:14:27.958000             🧑  作者: Mango
在 C# 中,Math.Round() 方法用于将指定的浮点数舍入为最接近的整数或指定小数位数的数字。它是 .NET Framework 中 Math 类的一个静态方法。
public static double Round(double value)
public static double Round(double value, int digits)
public static decimal Round(decimal value)
public static decimal Round(decimal value, int digits)
public static double Round(double value, MidpointRounding mode)
public static double Round(double value, int digits, MidpointRounding mode)
public static decimal Round(decimal value, MidpointRounding mode)
public static decimal Round(decimal value, int digits, MidpointRounding mode)
double num1 = 12.345;
double result1 = Math.Round(num1);
Console.WriteLine(result1); // 输出:12
decimal num2 = 12.6789m;
decimal result2 = Math.Round(num2);
Console.WriteLine(result2); // 输出:13
double num3 = 12.345;
double result3 = Math.Round(num3, 2);
Console.WriteLine(result3); // 输出:12.35
decimal num4 = 12.6789m;
decimal result4 = Math.Round(num4, 1);
Console.WriteLine(result4); // 输出:12.7
double num5 = 12.345;
double result5 = Math.Round(num5, MidpointRounding.AwayFromZero);
Console.WriteLine(result5); // 输出:12
decimal num6 = 12.6789m;
decimal result6 = Math.Round(num6, 1, MidpointRounding.ToEven);
Console.WriteLine(result6); // 输出:12.7
有关 Math.Round() 方法的更多详细信息,请参阅 Microsoft 文档。
希望这篇介绍对你有所帮助!