1 import type { Currency } from '../interfaces';
5 * 600 -> 6, 650 -> 6.50, 633 -> 6.33
7 const humanPrice = (amount: number = 0, divisor: number = 100) => {
8 const fixedValue = Number(amount / divisor).toFixed(2);
9 return fixedValue.replace('.00', '').replace('-', '');
12 export default humanPrice;
14 export const humanPriceWithCurrency = (amount: number, currency: Currency, divisor?: number) => {
15 if (typeof amount !== 'number' || typeof currency !== 'string') {
16 throw new Error('humanPriceWithCurrency: Invalid parameters');
19 const value = humanPrice(amount, divisor);
20 const isNegative = amount < 0;
21 const prefix = isNegative ? '-' : '';
23 if (currency === 'EUR') {
24 return `${prefix}${value} €`;
27 if (currency === 'CHF') {
28 return `${prefix}CHF ${value}`;
31 if (currency === 'USD') {
32 return `${prefix}$${value}`;
35 return `${prefix}${value}`;