Skip to content

[Repo Assist] [JS/TS] Fix sprintf %g/%G not stripping trailing zeros when explicit precision is given#4543

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
repo-assist/fix-printf-g-trailing-zeros-80d5cf0dc636481e
Draft

[Repo Assist] [JS/TS] Fix sprintf %g/%G not stripping trailing zeros when explicit precision is given#4543
github-actions[bot] wants to merge 1 commit intomainfrom
repo-assist/fix-printf-g-trailing-zeros-80d5cf0dc636481e

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

sprintf "%.6g" 5. was emitting "5.00000" instead of "5". The C printf %g format (which F# sprintf %g follows) always strips trailing zeros from the mantissa.

Root Cause

In formatReplacement() (the printf handler), the %g/%G case called toPrecision(rep, precision) but didn't apply any trailing-zero stripping — unlike the String.format() handler ({0:G} style) at line 393, which already had trimEnd(trimEnd(rep, "0"), ".").

The bug only affects %g with an explicit precision (e.g. %.6g, %.10g). Without an explicit precision, toPrecision() with no argument calls JS .toPrecision(undefined) which is equivalent to .toString() and doesn't pad.

Fix

Added trailing-zero stripping to the %g/%G case in formatReplacement(), using a regex to correctly handle scientific-notation output (e.g. "1.0000e-10""1e-10") before the trimEnd pass.

Also improved the existing String.format() G case to use the same regex-first approach, removing the stale TODO comment.

Test

Added testCase "sprintf %g strips trailing zeros" to tests/Js/Main/StringTests.fs covering:

  • sprintf "%.6g" 5. = "5" (was "5.00000")
  • sprintf "%.10g" 1.23 = "1.23" (was "1.2300000000")
  • sprintf "%.6g" 0.0001 = "0.0001"
  • sprintf "%g" 123.456 = "123.456" (no precision — unchanged)
  • sprintf "%.4g" 1234.5 = "1234"

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@8f2d44f5d624e31c4af7cdbd948c6f5eba875a85

When using an explicit precision like `sprintf "%.6g" 5.`, the output
was "5.00000" instead of the expected "5". The C printf %g format
always strips trailing zeros from the mantissa.

Also fix the G format in String.format() to correctly handle scientific
notation (e.g. "1.0000e-10" -> "1e-10") by using regex substitution
before the trimEnd calls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added automation Automated changes repo-assist Created by Repo Assist labels Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation Automated changes repo-assist Created by Repo Assist

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants