Double-to-string format using "G17" does not match .NET #2654
Copy link
Copy link
Closed
Labels
Description
Description
Using the "G17" float format doesn't match .NET.
Repro code
let f (x: float) = printfn "%s" (x.ToString("G17"))
f 123.45
f 10_000.0
f 0.000222Expected and actual results
.NET output:
123.45
10000
0.000222
Fable output:
123.45000000000000
10000.000000000000
0.00022200000000000000
It seems that .NET is just omitting the trailing zeros? Here are the docs, for reference: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#GFormatString
As a workaround, I'm just using ToString without a format specifier and it seems to be working for my purposes for now. I was just needing a float format that could round-trip, and MS docs recommend you use "G17", but I don't seem to be having any issues with whatever the default is yet.
Related information
- Fable version: 3.6.3
- .NET version: 6.0.100
- Operating system: Windows 10.0.19043
Reactions are currently unavailable
Activity
github-actions commentedon Apr 20, 2026
fix(js/ts): fix G/g format specifier corrupting exponential notation …
fable-repo-assist commentedon May 2, 2026
🤖 This is an automated response from Repo Assist.
I've investigated this issue and created a fix PR.
Root cause: The
G/gformat specifier handler inString.tscallstoPrecision()which can return results in exponential notation (e.g.,"1.0000000000000000e-10"). The subsequenttrimEnd(rep, "0")was applied to the full string, incorrectly stripping the trailing"0"from the exponent-10, turning it into-1.Fix: Split on the
"e"separator first, trim trailing zeros only from the mantissa, then recombine with the exponent. Additionally, uppercase"G"now correctly produces uppercase"E"per .NET semantics.A draft PR has been opened with the fix and regression tests for the cases in this report plus the exponential notation edge case.