+ 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
5 ответов
+ 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)));
+ 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.
+ 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);
+ 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%.
+ 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);
}