Skip to content

Instantly share code, notes, and snippets.

@dehghani-mehdi
Last active November 12, 2020 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dehghani-mehdi/ac42e5808f3292d311786478814ee274 to your computer and use it in GitHub Desktop.
Save dehghani-mehdi/ac42e5808f3292d311786478814ee274 to your computer and use it in GitHub Desktop.
Get discount price and discount percentage in C# and JavaScript

C#

public decimal GetDiscountPercentage(decimal sellPrice, decimal discountPrice)
    => sellPrice == 0 ? 0 : Math.Abs((((discountPrice - sellPrice) / sellPrice) * 100));

public decimal GetDiscountPrice(decimal sellPrice, byte discountPercentage)
    => discountPercentage == 0 ? 0 : sellPrice == 0 ? 0 : sellPrice - (sellPrice * discountPercentage / 100);

JS

var getDiscountPercentage = function (sellPrice, discountPrice) {
    return sellPrice === 0 ? 0 : Math.abs((((discountPrice - sellPrice) / sellPrice) * 100));
}

var getDiscountPrice = function (sellPrice, discountPercentage) {
    return discountPercentage === 0 ? 0 : sellPrice === 0 ? 0 : sellPrice - (sellPrice * discountPercentage / 100);
}

JS - ES6

const getDiscountPercentage = (sellPrice, discountPrice)
    => sellPrice === 0 ? 0 : Math.abs((((discountPrice - sellPrice) / sellPrice) * 100));

const getDiscountPrice = (sellPrice, discountPercentage)
    => discountPercentage === 0 ? 0 : sellPrice === 0 ? 0 : sellPrice - (sellPrice * discountPercentage / 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment