Skip to content

Avoid precision loss in formatUnits#255

Open
samsamtrum wants to merge 1 commit into
pushchain:mainfrom
samsamtrum:fix-format-units-precision
Open

Avoid precision loss in formatUnits#255
samsamtrum wants to merge 1 commit into
pushchain:mainfrom
samsamtrum:fix-format-units-precision

Conversation

@samsamtrum

Copy link
Copy Markdown

helpers.formatUnits currently applies the optional precision by converting the formatted decimal string through parseFloat. That can lose precision for large token amounts before truncation.

This keeps the precision truncation on the decimal string instead, matching the existing truncate-not-round behavior while preserving large values exactly.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9f0d3514be

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return integerPart;
}

return `${integerPart}.${truncatedFraction.replace(/0+$/, '')}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Return zero instead of a dangling decimal

When the requested precision keeps only zero fractional digits, truncatedFraction is truthy before the trailing-zero trim, so this returns a dangling decimal point. For example, formatUnits('1', { decimals: 18, precision: 2 }) now returns "0." instead of the previous/expected "0", which breaks small values below the display precision.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant