Skip to content

Commit

Permalink
refactor format for small value
Browse files Browse the repository at this point in the history
  • Loading branch information
clbrge committed Apr 20, 2023
1 parent 7f35916 commit d9c0d20
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ export class Token {

// return string
format(number, decimals = this.decimals) {
return this.#iformat
.format(ethers.formatUnits(number, decimals))
.replace('ETH', this.formatSymbol || this.symbol || this.name || '?')
const f = Number(ethers.formatUnits(number, decimals))
const s = this.formatSymbol || this.symbol || this.name || '?'
return f > 0.1
? this.#iformat
.format(ethers.formatUnits(number, decimals))
.replace('ETH', s)
: `${s} ${f}`
}

// return BigInt
Expand Down
13 changes: 9 additions & 4 deletions test/erc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,17 @@ describe('TokenERC20Amount', function () {
})

// TODO
describe('Misc', function () {
it('xxx', async function () {
console.log(ethers.formatUnits('1000000000000', 18))
const amountA = await TokenAmount.from(usdc, 3)
xdescribe('Misc', function () {
it('big', async function () {
const amountA = await TokenAmount.from(usdc, 100000)
console.log(amountA.toFloat())
console.log(amountA.toString())
})
it('small', async function () {
const amountA = await TokenAmount.from(usdc, 0.00001)

console.log(amountA.toFloat())
console.log(amountA.toString())
})
})

Expand Down

0 comments on commit d9c0d20

Please sign in to comment.