C# 如何将 Math.Ceiling 结果转换为 int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8832978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:42:43  来源:igfitidea点击:

how to convert Math.Ceiling result to int?

c#

提问by javapowered

Math.Ceilingreturns doublebecause doublemay store much bigger numbers. However if i'm sure that inttype is capable to store the result how should I convert? Is it safe to cast (int) Math.Ceiling(...?

Math.Ceiling返回,double因为double可能存储更大的数字。但是,如果我确定该int类型能够存储结果,我应该如何转换?投射是否安全(int) Math.Ceiling(...

采纳答案by ?yvind Br?then

If you are sure that you do not cross the capacity of int, it should be perfectly safe to do

如果您确定没有超过 int 的容量,那么这样做应该是完全安全的

int myInt = (int)Math.Ceiling(...);

If you are not sure about the bound, you could go with longinstead of int.

如果您不确定界限,则可以使用long代替int.

回答by Fredrik Johansson

I'd go with

我愿意

int x = (int)Math.Ceiling(0.9); // 1

回答by John Woo

int oInt = Convert.ToInt32(Math.Ceiling(value));

int oInt = Convert.ToInt32(Math.Ceiling(value));

since Math.Ceilingreturns doubleand you want to convert it to int, use ConvertClass.
example:

由于Math.Ceiling返回double并且您想将其转换为int,请使用ConvertClass。
例子:

double[] values= { Double.MinValue, -1.38e10, -1023.299, -12.98,
                   0, 9.113e-16, 103.919, 17834.191, Double.MaxValue };
int result;

foreach (double value in values)
{
   try {
      result = Convert.ToInt32(value);
      Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
                        value.GetType().Name, value,
                        result.GetType().Name, result);
   }
   catch (OverflowException) {
      Console.WriteLine("{0} is outside the range of the Int32 type.", value);
   }   
}                                 
//    -1.79769313486232E+308 is outside the range of the Int32 type.
//    -13800000000 is outside the range of the Int16 type.
//    Converted the Double value '-1023.299' to the Int32 value -1023.
//    Converted the Double value '-12.98' to the Int32 value -13.
//    Converted the Double value '0' to the Int32 value 0.
//    Converted the Double value '9.113E-16' to the Int32 value 0.
//    Converted the Double value '103.919' to the Int32 value 104.
//    Converted the Double value '17834.191' to the Int32 value 17834.
//    1.79769313486232E+308 is outside the range of the Int32 type.

回答by Mharlin

If you are uncertain you can always put an if statement and check if the number you get back is highter then int.MaxValue

如果您不确定,您可以随时添加 if 语句并检查您返回的数字是否更高 int.MaxValue

回答by Aurimas Neverauskas

From C++ practices, I would use the following. It's guaranteed to get the correct result even when ceiling returns 99.99999...8 or 100.000000...1

从 C++ 实践中,我将使用以下内容。即使天花板返回 99.99999...8 或 100.000000...1 也能保证得到正确的结果

var result = (int)(Math.Ceiling(value) + 0.5);

The code below should work too if you trust its implementation

如果您相信它的实现,下面的代码也应该可以工作

var result = Convert.ToInt32(value);

回答by Todd

If it's all about speed, then Math.Ceiling for Int inputs and output is quite slow. The fastest is an inline expression:

如果一切都与速度有关,那么用于 Int 输入和输出的 Math.Ceiling 会非常慢。最快的是内联表达式:

var ceilingResult = (value / divisor) + (value % divisor == 0 ? 0 : 1);

From my own benchmark of 10M iterations, Math.Ceiling takes ~2.4 seconds. Calling this expression inside a named function takes ~380 ms and having it as a direct inline expression takes ~33ms.

从我自己的 1000 万次迭代基准来看,Math.Ceiling 需要大约 2.4 秒。在命名函数内调用此表达式需要约 380 毫秒,而将其作为直接内联表达式需要约 33 毫秒。

If you would like Math.Floor for Int inputs and Output, it's even easier:

如果您希望使用 Math.Floor 进行 Int 输入和输出,则更简单:

var floorResult = (value / divisor);