In our code we have a double that we need to convert to an int.
double score = 8.6;
int i1 = Convert.ToInt32(score);
int i2 = (int)score;
Can anyone explain me why i1 != i2?
The result that I get is that: i1 = 9 and i2 = 8.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because
Convert.ToInt32rounds:…while the cast truncates:
Update: See Jeppe Stig Nielsen’s comment below for additional differences (which however do not come into play if
scoreis a real number as is the case here).