mafortis
mafortis

Reputation: 7118

Get discount percentage in angular

I have line of code where I try to get discounted price in percentage but it returns opposite.

{{((product.newprice/product.price)*100).toFixed(0)}}%

This supposed to return 3% for instance but it returns 97% instead. I've tried to move codes around to fix it but no luck.

Example

price = 25.499.000
newprice = 24.750.000

result should be: 3% discount

Any idea what I did wrong here?

Upvotes: 0

Views: 2227

Answers (2)

Sanchit
Sanchit

Reputation: 51

One of the most Easiest Way to caculate discount percentage is -

 {{100-((item.price/item.mrp)*100) }}

and in your Case -

{{100-((product.newprice/product.price)*100) }}

or else in Angular you can Use Pipe

{{((product.price-product-newprice)/product.price) |percent }}

Upvotes: 0

frodo2975
frodo2975

Reputation: 11745

Your math is wrong. The discount is the original price minus the sale price, so your equation should be ((product.price - product.newprice)/product.price)*100. Note that this will give you negative discounts if the new price is ever higher than the original.

Upvotes: 3

Related Questions