From ac7c18bc0f0e008b8db9fd4b69451a89e7fb069b Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Wed, 1 Mar 2023 23:02:54 +0330 Subject: [PATCH] feat(math): calcDiscount --- core/math/src/math.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/math/src/math.ts b/core/math/src/math.ts index 9d4ab94b7..ff6b205f4 100644 --- a/core/math/src/math.ts +++ b/core/math/src/math.ts @@ -252,3 +252,8 @@ export const hex = (bytes: Uint8Array): string => { } return str; }; + +export const calcDiscount = (price: number, finalPrice: number, decimal = 2, upSide = true): number => { + decimal = Math.pow(10, decimal); + return Math.round((price - finalPrice) / (upSide ? finalPrice : price) * 100 * decimal) / decimal; +};