23 lines
528 B
TypeScript
23 lines
528 B
TypeScript
export function toCents(amount: number): number {
|
|
return Math.round(amount * 100);
|
|
}
|
|
|
|
export function fromCents(cents: number): number {
|
|
return cents / 100;
|
|
}
|
|
|
|
const LOCALE_BY_CURRENCY: Record<string, string> = {
|
|
EUR: 'de-DE',
|
|
USD: 'en-US',
|
|
GBP: 'en-GB',
|
|
CHF: 'fr-CH',
|
|
};
|
|
|
|
export function formatCurrency(cents: number, currency: string): string {
|
|
const locale = LOCALE_BY_CURRENCY[currency] ?? 'de-DE';
|
|
return new Intl.NumberFormat(locale, {
|
|
style: 'currency',
|
|
currency,
|
|
}).format(fromCents(cents));
|
|
}
|