Skip to content

Double-to-string format using "G17" does not match .NET #2654

Closed

Description

@jwosty

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.000222

Expected 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

Activity

  1. github-actions commented on Apr 20, 2026

    @github-actions
  2. added a commit that references this issue on May 2, 2026
    76aa0d6
  3. fable-repo-assist commented on May 2, 2026

    @fable-repo-assist
    Contributor

    🤖 This is an automated response from Repo Assist.

    I've investigated this issue and created a fix PR.

    Root cause: The G/g format specifier handler in String.ts calls toPrecision() which can return results in exponential notation (e.g., "1.0000000000000000e-10"). The subsequent trimEnd(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.

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

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@51c8f6ad4357d2ecc06e47120031b3d75e80227d
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions