c#四舍五入的方法
方法一:使用Math.Round()函数
double number = 3.14159;
double result = Math.Round(number, 2); // 四舍五入保留两位小数
Console.WriteLine(result); // 输出: 3.14
方法二:使用Math.Floor()和Math.Ceiling()函数
double number = 3.14159;
double roundedDown = Math.Floor(number * 100) / 100; // 向下取整保留两位小数
double roundedUp = Math.Ceiling(number * 100) / 100; // 向上取整保留两位小数
Console.WriteLine(roundedDown); // 输出: 3.14
Console.WriteLine(roundedUp); // 输出: 3.15
方法三:使用.ToString()方法和字符串格式化
double number = 3.14159;
double rounded = Math.Round(number, 2);
string result = rounded.ToString("0.00"); // 格式化为保留两位小数的字符串
Console.WriteLine(result); // 输出: 3.14