Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
double a = 3.99;
double b = -3.99;
Console.WriteLine((int)a);
Console.WriteLine((int)b);3 -3
- Expecting
4 -4: a cast tointdoes not round to the nearest whole number; it drops the fraction, so3.99becomes3. - Expecting
-4for the negative number: dropping the fraction moves a number toward zero, so-3.99becomes-3, not-4.
- What does
Convert.ToInt32(3.99)return, and why does it differ from the cast? - Why does
int x = 3.99;not compile whileint x = (int)3.99;does?