[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
Conversation
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>
41 tasks
24 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
sprintf "%.6g" 5.was emitting"5.00000"instead of"5". The Cprintf%gformat (which F#sprintf %gfollows) always strips trailing zeros from the mantissa.Root Cause
In
formatReplacement()(the printf handler), the%g/%Gcase calledtoPrecision(rep, precision)but didn't apply any trailing-zero stripping — unlike theString.format()handler ({0:G}style) at line 393, which already hadtrimEnd(trimEnd(rep, "0"), ".").The bug only affects
%gwith 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/%Gcase informatReplacement(), using a regex to correctly handle scientific-notation output (e.g."1.0000e-10"→"1e-10") before thetrimEndpass.Also improved the existing
String.format()G case to use the same regex-first approach, removing the staleTODOcomment.Test
Added
testCase "sprintf %g strips trailing zeros"totests/Js/Main/StringTests.fscovering: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"