Casting a double as an int, does it round or just strip digits?

It does not round, it just returns the integral part before the decimal point.

Reference (thanks Rawling) Explicit Numeric Conversions Table:

When you convert a double or float value to an integral type, this
value is rounded towards zero to the nearest integral value.

You can try simple issues like this by yourself by writing simple tests. The following test (using NUnit) will pass and therefore give an answer to your question:

[Test]
public void Cast_float_to_int_will_not_round_but_truncate
{
    var x = 3.9f;
    Assert.That((int)x == 3); // <-- This will pass
}

Leave a Comment

File not found.