This has a potentially simple answer but I can’t figure it out –
double Result = 1 / 12;
returns 0, while
double Result2 = 24 / 12;
return 2
What’s going on and how can I fix it?
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.
Try this:
or this:
In C# (and also in a lot of other languages), integer division returns an integer. By casting one of the operands to
doubleor explicitly declaring a literaldoubleyou can force the division expression to return adoubleand not truncate after the decimal place.