简体   繁体   中英

Get the percentage of the discount value in c#

In my applicattion i want to find how much in percent is the discount, I have the price of an item then when i give a new price and find the Subtraction I want to know how much is that in percent, example:

decimal realPrice = 1800;
decimal newPrice = 1700;
decimal difference = realPrice - newPrice; // = 100
decimal discountPercent = ????;

How many percent is this diffence as a discount in percent from the realprice. Thank you in advance.

You should divide difference by realPrice and then multiply it by 100 to get percentages

decimal discountPercent = difference / realPrice * 100;

It should give you 5.(5)%

See this working example

decimal discountPercent = (difference / realPrice) * 100;
Console.WriteLine("Discount Percentage : {0}%",discountPercent.ToString("#.##"));

Little maths for explanation:

decimal discountPercent = difference / realPrice * 100;

Though this is not a programming question, but rather math question for middle school. :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM