Skip to content

Commit

Permalink
Merge pull request #512 from alephium/XPLR-fix-smart-rounding
Browse files Browse the repository at this point in the history
Fix smart rounding
  • Loading branch information
mvaivre authored Apr 10, 2024
2 parents 677979c + 5189168 commit f4aff42
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-schools-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alephium/shared": patch
---

Fix smart rounding
7 changes: 3 additions & 4 deletions packages/shared/src/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,9 @@ const smartRound = (amountString: string) => {
const secondSignigicantDigit = parseInt(amountString.charAt(indexOfFirstNonZero + 1))

if (secondSignigicantDigit >= 5) {
return (
amountString.slice(0, indexOfFirstNonZero - 1) +
(firstSignificantDigit === 9 ? '1' : (firstSignificantDigit + 1).toString())
)
return firstSignificantDigit === 9
? amountString.slice(0, indexOfFirstNonZero - 1) + '1'
: amountString.slice(0, indexOfFirstNonZero) + (firstSignificantDigit + 1).toString()
}

return amountString.slice(0, indexOfFirstNonZero + 1)
Expand Down
7 changes: 5 additions & 2 deletions packages/shared/test/numbers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,16 @@ it('Should abbreviate token amount', () => {
'0.0000000000000001'
),
expect(formatAmountForDisplay({ amount: BigInt('15'), amountDecimals: 17, smartRounding: true })).toEqual(
'0.000000000000002'
'0.0000000000000002'
),
expect(formatAmountForDisplay({ amount: BigInt('99100000'), amountDecimals: 17, smartRounding: true })).toEqual(
'0.000000001'
),
expect(formatAmountForDisplay({ amount: BigInt('4590000000'), amountDecimals: 17, smartRounding: false })).toEqual(
'0.0000000459'
),
expect(formatAmountForDisplay({ amount: BigInt('4590000000'), amountDecimals: 17, smartRounding: true })).toEqual(
'0.0000005'
'0.00000005'
),
expect(formatAmountForDisplay({ amount: BigInt('4110000000'), amountDecimals: 17, smartRounding: true })).toEqual(
'0.00000004'
Expand Down

0 comments on commit f4aff42

Please sign in to comment.