如果您想要默认的truncate-towards-
零行为,则可以使用强制类型转换。或者,你可能想使用
Math.Ceiling,
Math.Round,
Math.Floor等-但你仍然需要铸造之后。
不要忘记的范围
int比的范围小得多
double。在未经检查的上下文中,如果值超出的范围,则从
doubleto
强制转换
int不会引发异常
int,而对
Convert.ToInt32(double)will
的调用。如果值超出范围,则强制转换的结果(在未经检查的上下文中)将显式未定义。

如果您想要默认的truncate-towards-
零行为,则可以使用强制类型转换。或者,你可能想使用
Math.Ceiling,
Math.Round,
Math.Floor等-但你仍然需要铸造之后。
不要忘记的范围
int比的范围小得多
double。在未经检查的上下文中,如果值超出的范围,则从
doubleto
int不会引发异常
int,而对
Convert.ToInt32(double)will