+ 1

C# Last task

The Latte with Value of 70 seems to be throwing an error when the discount is applied. If I "adjust" the value to get that right for case 1, then case 2 is incorrect. Is my calculation wrong? https://code.sololearn.com/cS85DddgtCgr/?ref=app

20th Nov 2021, 12:17 AM
Ausgrindtube
Ausgrindtube - avatar
5 Answers
+ 2
Ausgrindtube Ok if you want to do like that then do Math.Ceiling in final discounted amount. foreach(string s in coffee.Keys.ToArray()) Console.WriteLine(s + ": " + Convert.ToInt32(Math.Ceiling(coffee[s] * newdiscount1)));
20th Nov 2021, 6:39 AM
A͢J
A͢J - avatar
+ 2
Thank you A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟. I'll try it. May I ask why the Math.Ceiling would be necessary? (I don't think it has been mentioned in the course) I'll also look up the doc at Microsoft. ***UPDATE*** Thanks! That has given the all-green!! Now, I have to really try to understand why the Math.Ceiling made the difference. You're a legend and I really appreciate your help.
20th Nov 2021, 11:03 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Ausgrindtube Discount should be apply on each price. "Write a program to discount all of the prices" How 100 came? Discount formula: discountedAmount = (price - price * discount / 100); since calculation will come in double value so you need to cast with int int discountedAmount = coffee[s] - (int)(coffee[s] * discount / 100);
20th Nov 2021, 1:35 AM
A͢J
A͢J - avatar
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ "How 100 came?" Do you mean the "100 - discount amount" part? For me, and maybe I'm wrong, but if I'm calculating a discounted price, the better/more accurate way is to multiply by the "remaining part of the whole". For example: 30% discount means I want 70% of the total price. Which is then 100% - 30%. So (original price) x 70%.
20th Nov 2021, 3:30 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Ausgrindtube It is not necessary to use Math.Ceiling if you do like this: foreach (string c in coffee.Keys) { int res = (coffee[c] * discount / 100); res = coffee[c] - res; Console.WriteLine(c + ": " + res); }
20th Nov 2021, 11:48 AM
A͢J
A͢J - avatar