Files
CRM_Montzar/apps/web/src/lib/money.ts
T

42 lines
1.6 KiB
TypeScript
Raw Normal View History

export const CURRENCY_OPTIONS = [
{ code: 'IRR', labelFa: 'ریال ایران', suffix: 'ریال' },
{ code: 'USD', labelFa: 'دلار آمریکا', suffix: 'دلار' },
{ code: 'EUR', labelFa: 'یورو', suffix: 'یورو' },
{ code: 'AED', labelFa: 'درهم امارات', suffix: 'درهم' },
{ code: 'TRY', labelFa: 'لیر ترکیه', suffix: 'لیر' },
{ code: 'GBP', labelFa: 'پوند انگلیس', suffix: 'پوند' },
] as const;
export function currencySuffix(code: string): string {
return CURRENCY_OPTIONS.find((c) => c.code === code)?.suffix ?? code;
}
export function formatMoney(amount: number, currency = 'IRR'): string {
try {
return (
new Intl.NumberFormat('fa-IR').format(amount) +
' ' +
currencySuffix(currency)
);
} catch {
return `${amount} ${currency}`;
}
}
/** مثل ماکاپ: ۲۴۰ میلیون ریال */
export function formatCompactMoney(amount: number, currency = 'IRR'): string {
const suffix = currencySuffix(currency);
const abs = Math.abs(amount);
if (abs >= 1_000_000_000) {
const n = amount / 1_000_000_000;
const rounded = Math.abs(n - Math.round(n)) < 0.05 ? Math.round(n) : Math.round(n * 10) / 10;
return `${new Intl.NumberFormat('fa-IR', { maximumFractionDigits: 1 }).format(rounded)} میلیارد ${suffix}`;
}
if (abs >= 1_000_000) {
const n = amount / 1_000_000;
const rounded = Math.abs(n - Math.round(n)) < 0.05 ? Math.round(n) : Math.round(n * 10) / 10;
return `${new Intl.NumberFormat('fa-IR', { maximumFractionDigits: 1 }).format(rounded)} میلیون ${suffix}`;
}
return formatMoney(amount, currency);
}