Commit 5926d703 authored by lynn's avatar lynn Committed by GitHub

fix: handle tiny numbers (#4842)

* handle tiny numbers

* remove console
parent 52b51ee7
......@@ -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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment