37 lines
No EOL
1 KiB
TypeScript
37 lines
No EOL
1 KiB
TypeScript
import { getCurrencySymbol } from '~/utils/currency'
|
|
|
|
export function useCurrency() {
|
|
const coop = useCoopBuilder()
|
|
|
|
const currencySymbol = computed(() => getCurrencySymbol(coop.currency.value))
|
|
|
|
const formatCurrency = (amount: number, options?: { showSymbol?: boolean; precision?: number }) => {
|
|
const { showSymbol = true, precision = 0 } = options || {}
|
|
|
|
const formatted = new Intl.NumberFormat('en-US', {
|
|
minimumFractionDigits: precision,
|
|
maximumFractionDigits: precision
|
|
}).format(amount)
|
|
|
|
if (showSymbol) {
|
|
return `${currencySymbol.value}${formatted}`
|
|
}
|
|
|
|
return formatted
|
|
}
|
|
|
|
const formatCurrencyCompact = (amount: number) => {
|
|
if (amount >= 1000000) {
|
|
return `${currencySymbol.value}${(amount / 1000000).toFixed(1)}M`
|
|
} else if (amount >= 1000) {
|
|
return `${currencySymbol.value}${(amount / 1000).toFixed(1)}k`
|
|
}
|
|
return formatCurrency(amount)
|
|
}
|
|
|
|
return {
|
|
currencySymbol,
|
|
formatCurrency,
|
|
formatCurrencyCompact
|
|
}
|
|
} |