Skip to content

Commit

Permalink
Merge pull request #395 from reservoirprotocol/pedro/relay-5757-trail…
Browse files Browse the repository at this point in the history
…ing-zeros

Remove trailing zeros
  • Loading branch information
pedromcunha authored Dec 11, 2024
2 parents c45bb72 + aa034bd commit dde2269
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/heavy-carrots-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@reservoir0x/relay-kit-ui': patch
---

Remove trailing zeros from formatted amount
6 changes: 5 additions & 1 deletion packages/ui/src/utils/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ function truncateBalance(balance: string) {
* @param maxLength The maximum total length of the string representation.
* @returns A plain string representation of the number, trimmed to the specified length.
*/

function formatFixedLength(amount: string, maxLength: number) {
if (!/^[-+]?\d*\.?\d*$/.test(amount)) return 'Invalid number'

Expand All @@ -203,7 +204,10 @@ function formatFixedLength(amount: string, maxLength: number) {
}

// Ensure no unnecessary trailing zeros and remove any trailing decimal points
result = result.replace(/\.0+$/, '').replace(/\.$/, '')
result = result
.replace(/\.0+$/, '')
.replace(/\.(\d*[^0])0+$/, '.$1')
.replace(/\.$/, '')

// Add negative sign back if the number was negative
if (isNegative) {
Expand Down

0 comments on commit dde2269

Please sign in to comment.