fix: handle tiny numbers (#4842)
* handle tiny numbers * remove console
This commit is contained in:
parent
52b51ee7d0
commit
5926d7037d
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user