I\'m curious as to the best way to convert a double to an int. Runtime safety is my primary concern here (it doesn\'t necessarily have to be the fastest method, but that wou
If you really really need to find out if something went wrong, use a normal cast and check the result.
int ToInt(double foo)
{
int result = (int)foo;
if (foo != result)
throw new ArgumentException()
return result;
}
This will make sure no invalid conversion is done. If it is OK to round to nearest integer, use Math.Round
and check if result is within 0.5. This will make sure no NaN or infinity will get by your method.