diff --git a/.changeset/heavy-carrots-promise.md b/.changeset/heavy-carrots-promise.md new file mode 100644 index 00000000..a2fad1f3 --- /dev/null +++ b/.changeset/heavy-carrots-promise.md @@ -0,0 +1,5 @@ +--- +'@reservoir0x/relay-kit-ui': patch +--- + +Remove trailing zeros from formatted amount diff --git a/packages/ui/src/utils/numbers.ts b/packages/ui/src/utils/numbers.ts index 7ad1602a..501f67ac 100644 --- a/packages/ui/src/utils/numbers.ts +++ b/packages/ui/src/utils/numbers.ts @@ -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' @@ -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) {