fix: handle tiny numbers (#4842)

* handle tiny numbers

* remove console
This commit is contained in:
lynn 2022-10-07 14:43:52 -04:00 committed by GitHub
parent 52b51ee7d0
commit 5926d7037d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

@ -4,6 +4,14 @@ import { USDC_MAINNET } from 'constants/tokens'
import { currencyAmountToPreciseFloat, formatDollar } from './formatDollarAmt'
describe('currencyAmountToPreciseFloat', () => {
it('small number', () => {
const currencyAmount = CurrencyAmount.fromFractionalAmount(USDC_MAINNET, '20000', '7')
expect(currencyAmountToPreciseFloat(currencyAmount)).toEqual(0.00285)
})
it('tiny number', () => {
const currencyAmount = CurrencyAmount.fromFractionalAmount(USDC_MAINNET, '2', '7')
expect(currencyAmountToPreciseFloat(currencyAmount)).toEqual(0.000000285)
})
it('lots of decimals', () => {
const currencyAmount = CurrencyAmount.fromFractionalAmount(USDC_MAINNET, '200000000', '7')
expect(currencyAmountToPreciseFloat(currencyAmount)).toEqual(28.571)

@ -4,7 +4,11 @@ import numbro from 'numbro'
// Convert [CurrencyAmount] to number with necessary precision for price formatting.
export const currencyAmountToPreciseFloat = (currencyAmount: CurrencyAmount<Currency>) => {
return parseFloat(currencyAmount.toFixed(3))
const floatForLargerNumbers = parseFloat(currencyAmount.toFixed(3))
if (floatForLargerNumbers < 0.1) {
return parseFloat(currencyAmount.toSignificant(3))
}
return floatForLargerNumbers
}
// Using a currency library here in case we want to add more in future.