Home
Map
Cast to Int ExamplesCast doubles to ints using the explicit syntax form. See the results of casting to int.
C#
This page was last reviewed on Nov 1, 2023.
Cast int. In C# 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.
Cast
Conversion info. We show conversions from double, long and ulong to int. Casting a floating-point number removes the fractional part—so it rounds down (when positive).
Some input and output. Consider a double value with a fractional part, like 1.1. When we cast it, we expect to receive an integer equal to 1. Even 1.99 should result in 1.
(int)1.1 = 1 (int)1.5 = 1 (int)1.99 = 1 (int)-1.1 = -1
Example program. We perform many casts, and each is described in a separate section of the text. Casting some values can result in unexpected behavior.
Part 1 When you convert doubles to ints, the values after the decimal place will be dropped in the resulting value.
Part 2 You get an unusable result when you cast long and ulong values that cannot fit in an int.
long, ulong
Part 3 Large negative and positive doubles are cast to the same value, which is unusable.
double
// Part 1: cast 4 double values. { double value1 = 1.1; int value2 = (int)value1; System.Console.WriteLine("{0} -> {1}", value1, value2); } { double value1 = 1.5; int value2 = (int)value1; System.Console.WriteLine("{0} -> {1}", value1, value2); } { double value1 = 1.99; int value2 = (int)value1; System.Console.WriteLine("{0} -> {1}", value1, value2); } { double value1 = -1.1; int value2 = (int)value1; System.Console.WriteLine("{0} -> {1}", value1, value2); } // Part 2: cast long values. { long value1 = 10000000000; int value2 = (int)value1; System.Console.WriteLine("{0} -> {1}", value1, value2); } { ulong value1 = 10000000000; int value2 = (int)value1; System.Console.WriteLine("{0} -> {1}", value1, value2); } // Part 3: cast large double values. { double value1 = 10000000000; int value2 = (int)value1; System.Console.WriteLine("{0} -> {1}", value1, value2); } { double value1 = -10000000000; int value2 = (int)value1; System.Console.WriteLine("{0} -> {1}", value1, value2); }
1.1 -> 1 1.5 -> 1 1.99 -> 1 -1.1 -> -1 10000000000 -> 1410065408 10000000000 -> 1410065408 10000000000 -> -2147483648 -10000000000 -> -2147483648
Discussion. Casting to int is often done in C#. When we cast positive fractional values, they are rounded down to the nearest integer. This means even the value 1.99 is changed to 1.
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.
checked
A summary. The (int) cast is useful when converting from a double or other floating point value to an integer. A positive integer is never rounds up—this is sometimes problematic.
int, uint
For more sophisticated rounding, check out the Math.Round method in the System namespace. Casting to ints can be useful, but care must be used.
Math.Round
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 1, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.