Cast ints using the explicit syntax form. See the results of casting to int from different values.
Cast, int. Floating point values can be cast to ints. This has predictable results. We find out the result of a conversion to an integer from a value that cannot fit in an integer. We show conversions from double, long and ulong to int.Int, uintCasts
Expression and result table:(int)1.1 = 1
(int)1.5 = 1
(int)1.99 = 1
(int)-1.1 = -1
First, when you convert doubles to ints, the values after the decimal place will be dropped in the resulting value. This is similar to Math.Floor in this aspect. This is demonstrated with four double values.Math.Floor
Next: You get an unusable result when you cast long and ulong values that cannot fit in an int.
Finally: Large negative and positive doubles are cast to the same value, which is unusable.
Discussion. This is important because if you ever cast fractional values to integers, you will know they are always rounded down to the nearest integer. This means even the value 1.99 is changed to 1. The (int) cast never rounds the value up.
Also: When we cast longs, ulongs or doubles that cannot fit in the memory space of an int, we receive predictable but unusable results.
Tip: If you want to be alerted if such an error occurs, use the checked context. Also note the unchecked context to disable this.