双转换为int [英] convert double to int

查看:218
本文介绍了双转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是转换双为int的最佳途径。使用CAST?

What is the best way to convert a double to Int. Use cast?

推荐答案

您可以使用强制转换,如果你想默认截断向零行为。或者,你可能想使用 Math.Ceiling Math.Round Math.Floor 等等 - 但你仍然需要一个演员之后

You can use a cast if you want the default truncate-towards-zero behaviour. Alternatively, you might want to use Math.Ceiling, Math.Round, Math.Floor etc - although you'll still need a cast afterwards.

不要忘了 INT 的范围比的范围更小的双。强制转换从 INT 如果该值的范围之外会不会引发异常 INT 在一个未经检查的情况下,而呼叫 Convert.ToInt32(双)会。中投(在未经检查的情况下)的结果是明确的,如果没有定义的值范围之外。

Don't forget that the range of int is much smaller than the range of double. A cast from double to int won't throw an exception if the value is outside the range of int in an unchecked context, whereas a call to Convert.ToInt32(double) will. The result of the cast (in an unchecked context) is explicitly undefined if the value is outside the range.

这篇关于双转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆