fix(js/ts): fix G/g format specifier corrupting exponential notation when trimming trailing zeros#4587
Draft
fable-repo-assist[bot] wants to merge 2 commits intomainfrom
Conversation
…when trimming trailing zeros
The trimEnd() call applied to the full toPrecision() result was
incorrectly removing trailing zeros from the exponent part of numbers
in exponential notation. For example, (1e-10).ToString("G17") would
produce "1.0000000000000000e-1" instead of "1E-10" because trimEnd
matched the "0" in "-10".
Fix: split on the "e" separator, trim only the mantissa, then
recombine with the exponent. Uppercase "G" now also correctly produces
uppercase "E" in the exponent per .NET semantics.
Closes #2654
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Fixes a bug in the
G/gformat specifier for floating-point numbers where trailing zero trimming was incorrectly applied to numbers in exponential notation, corrupting the exponent.Closes #2654
Root Cause
In
String.ts, theG/gformat handler callstoPrecision(rep, precision)which can return results in exponential notation (e.g.,"1.0000000000000000e-10"). The subsequenttrimEnd(rep, "0")was applied to the full string, incorrectly trimming trailing zeros from the exponent part. For example:(1e-10).toPrecision(17)→"1.0000000000000000e-10"trimEnd(rep, "0")→"1.0000000000000000e-1"❌ (wrong: trimmed"0"from"-10")Fix
Split on the
"e"separator before trimming: trim trailing zeros only from the mantissa, then recombine with the exponent. Additionally, uppercase"G"now correctly produces uppercase"E"in the exponent per .NET semantics.Changes
src/fable-library-ts/String.ts: Fix theG/gcase in the format specifier switchtests/Js/Main/StringTests.fs: Add regression tests covering the cases from Double-to-string format using "G17" does not match .NET #2654 plus the exponential notation edge caseTests Added
CI will validate the build and tests.