asi-calculator/lib/numbers.ts
Matthias Kretschmann 0d08ba807b
better number formatting (#9)
* tweak number display

* select whole amount upon input focus

* handle letter input

* number formatting
2024-04-16 12:20:29 +02:00

23 lines
655 B
TypeScript

import { formatCurrency } from '@coingecko/cryptoformat'
export function formatCrypto(price: number, currency: string, locale: string) {
return formatCurrency(price, currency, locale, false, {
decimalPlaces: 3,
significantFigures: 1
})
}
export function formatFiat(price: number, currency: string, locale: string) {
let formattedPrice = formatCurrency(price, currency, locale, false, {
decimalPlaces: 2,
significantFigures: 8
})
// Add a trailing zero if only one digit after the decimal
if (formattedPrice.includes('.') && formattedPrice.split('.')[1].length < 2) {
formattedPrice += '0'
}
return formattedPrice
}