mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2025-07-29 14:00:05 +02:00

[web] Clean up localized currency format test (`local-ccy-format-v2`) GitOrigin-RevId: 30d671479522b87ee9205994508b745d2b0ae4c3
28 lines
653 B
TypeScript
28 lines
653 B
TypeScript
import getMeta from '@/utils/meta'
|
|
|
|
const DEFAULT_LOCALE = getMeta('ol-i18n')?.currentLangCode ?? 'en'
|
|
|
|
export function formatCurrency(
|
|
amount: number,
|
|
currency: string,
|
|
locale: string = DEFAULT_LOCALE,
|
|
stripIfInteger = false
|
|
): string {
|
|
const options: Intl.NumberFormatOptions = { style: 'currency', currency }
|
|
if (stripIfInteger && Number.isInteger(amount)) {
|
|
options.minimumFractionDigits = 0
|
|
}
|
|
|
|
try {
|
|
return amount.toLocaleString(locale, {
|
|
...options,
|
|
currencyDisplay: 'narrowSymbol',
|
|
})
|
|
} catch {}
|
|
|
|
try {
|
|
return amount.toLocaleString(locale, options)
|
|
} catch {}
|
|
|
|
return `${currency} ${amount}`
|
|
}
|