katanakit-js / useSalePrice
Function: useSalePrice()
ts
function useSalePrice(options): SalePrice;Defined in: src/core/services/formatter.service.ts:142
Pure: applies a discount to a price and reports what was taken off.
discount accepts either a percentage (40 → 40%) or a decimal fraction (0.4 → 40%) — the same convention as taxes in useFormatCurrency. Values > 1 are treated as percentages; values in (0, 1] as fractions. Prefer an explicit fraction when you need a rate ≤ 1%.
The returned salePrice is unrounded, so it survives markup chains (10% + 20% of the same base) without accumulating error; pass it straight to useFormatCurrency to get the digits the currency demands. Only percentOff is rounded, because it exists to be displayed.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | { discount: number; price: number; } | Price and discount rate. |
options.discount | number | Discount rate: percentage when > 1 (e.g. 40 → 40%), or decimal fraction when in (0, 1] (e.g. 0.4 → 40%). Same convention as taxes. |
options.price | number | Regular price, before the discount. |
Returns
The regular price, the discounted price and the rounded percentage.
Example
ts
import { useFormatCurrency, useSalePrice } from "katanakit-js";
const sale = useSalePrice({ price: 29.99, discount: 40 });
// { price: 29.99, salePrice: 17.994, percentOff: 40 }
useFormatCurrency({ amount: sale.salePrice, currency: "EUR", locale: "es" }); // "17,99 €"