Why my C# code returns 0, if it must be 50 ?
double c = (1 / 2) * 100;
Console.WriteLine(c);
what is wrong?
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.
1,2and100, in your example, are allintliterals. In C#, integer division returns integers, and ignores the remainder. Only after calculating 1/2 (=0) and multiplying by 100 (=0) does the result get converted todoubleWould give the expected result, because now
1.0is adoubleliteral, and forces the other literals to be converted todoubles also before the calculations are performed.