Why does the following code:
Console.WriteLine(String.Format("{0:C0}", 2170/ 20));
yield “$109”, while doing
Console.WriteLine(Math.Round(2170 / 20));
gives me 108?
How can I get 2170 / 20 give me 109?
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.
When you divide to values of integral type, such as
2170and20, the runtime performs an integer division and discards (truncates) the decimal.If you change one of the operands to a
float,double, ordecimal(eg,2170.0 / 20, or2170 / 20m), it will perform a floating-point division, as you would expect.Therefore, you need to change it to
EDIT
Like this: