在 C# 中,如何将 float 向上舍入到最接近的整数?
我看到 Math.Ceiling 和 Math.Round,但它们返回的是小数。我是否使用其中之一然后转换为 Int?
最佳答案
如果你想舍入到最近的整数:
int rounded = (int)Math.Round(precise, 0);
您还可以使用:
int rounded = Convert.ToInt32(precise);
这将使用 Math.Round(x, 0);
为您进行舍入和转换。它看起来更整洁,但在 IMO 上不太清晰。
如果您想向上取整:
int roundedUp = (int)Math.Ceiling(precise);
关于c# - 如何在 C# 中将 float 向上舍入到最接近的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/904910/