22 lines
789 B
C#
22 lines
789 B
C#
namespace Prices;
|
|
|
|
/// <summary>Turns a list price into what the customer actually pays.</summary>
|
|
public static class PriceCalculator
|
|
{
|
|
/// <summary>Applies a percentage discount to a price, e.g. 25 for a quarter off.</summary>
|
|
public static decimal ApplyDiscount(decimal price, decimal discountPercent)
|
|
{
|
|
if (price < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(price), "A price cannot be negative.");
|
|
}
|
|
|
|
if (discountPercent is < 0 or > 100)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(discountPercent), "A discount is between 0 and 100 percent.");
|
|
}
|
|
|
|
var raw = price * (100 - discountPercent) / 100;
|
|
return Math.Round(raw / 0.05m, MidpointRounding.AwayFromZero) * 0.05m;
|
|
}
|
|
} |