Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
5926d703
Unverified
Commit
5926d703
authored
Oct 07, 2022
by
lynn
Committed by
GitHub
Oct 07, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: handle tiny numbers (#4842)
* handle tiny numbers * remove console
parent
52b51ee7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
1 deletion
+13
-1
formatDollarAmt.test.ts
src/utils/formatDollarAmt.test.ts
+8
-0
formatDollarAmt.ts
src/utils/formatDollarAmt.ts
+5
-1
No files found.
src/utils/formatDollarAmt.test.ts
View file @
5926d703
...
...
@@ -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
)
...
...
src/utils/formatDollarAmt.ts
View file @
5926d703
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment