Files
evidencija-rezija/app/lib/formatStrings.ts

43 lines
1.4 KiB
TypeScript

export const formatCurrency = (amount: number, currencyCode?: string | null) => {
// format number with 2 decimal places and a thousand separator
// amount is in cents
const amountInUnits = amount / 100;
// Use Intl.NumberFormat for proper currency formatting
try {
return new Intl.NumberFormat('hr-HR', {
style: 'currency',
currency: currencyCode ?? "EUR",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(amountInUnits);
} catch (e) {
// Fallback if invalid currency code
console.error(`Invalid currency code: ${currencyCode}`, e);
}
}
/**
* Formats an IBAN for display with proper spacing
* Format: XX12 XXXX XXXX XXXX XXXX XX
* First group: 2 letters (country) + 2 digits (check) = 4 chars
* Following groups: 4 characters each
* Last group: can be less than 4 characters
* @param iban - IBAN string without spaces
* @returns Formatted IBAN with spaces
*/
export const formatIban = (iban: string | null | undefined): string => {
if (!iban) return "";
// Remove any existing spaces
const cleaned = iban.replace(/\s/g, '');
// Split into groups: first 4 chars, then groups of 4
const groups: string[] = [];
for (let i = 0; i < cleaned.length; i += 4) {
groups.push(cleaned.slice(i, i + 4));
}
return groups.join(' ');
}