sprintf and printfn float formats %e, %g, and %f differ from .NET

Open
#5,281 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
backend

Research direction

Start by locating Fable's implementation of sprintf and printfn float formatting, then compare its %e, %g, and %f behavior with the .NET expectations and repro cases in the issue. Run the relevant formatting tests and add coverage for the listed edge cases, including precision zero and negative zero. Done when the outputs match the expected results and %.0g no longer throws.

Written by the indexing model from the issue text.

Description

Description

In Fable 5.27.0 targeting JavaScript/TypeScript, sprintf/printfn float format specifiers %e/%E, %g/%G, and %f diverge from .NET (String.Format in the same runtime already follows the .NET rules), and %.0g throws a RangeError (regression from 5.26.0).

Per the F# plaintext formatting reference, %e/%E defaults to precision 6 with a 3-digit exponent and matching case, %g/%G strips trailing zeros (treating precision 0 as default), %f never switches to exponential notation, and negative zero keeps its - sign.

Repro code
for s in [
    sprintf "%e" 3.0
    sprintf "%E" 3.0
    sprintf "%.2g" 1e301
    sprintf "%.8g" 0.0
    sprintf "%.4e" -0.0
    sprintf "%f" -0.0
    sprintf "%.0f" 1e21
    sprintf "%.0g" 2.5
] do
    printfn "%s" s
Expected and actual results

Expected (.NET 10):

3.000000e+000
3.000000E+000
1e+301
0
-0.0000e+000
-0.000000
1000000000000000000000
2.5

Actual under Fable 5.27.0 (Node.js v24.14.1): the first seven lines print 3e+0, 3e+0, 1.0e+301, 0.0000000, 0.0000e+0, 0.000000, and 1e+21, and sprintf "%.0g" 2.5 throws RangeError: toPrecision() argument must be between 1 and 100.

Related information
  • Fable version: 5.27.0
  • Operating system: macOS 26.3 (Node.js v24.14.1, .NET 10)

Credit: https://ucsbplse.github.io/

Dominant language
F#
Stars
3.2k
Forks
335
Avg merge
18h 57m
Merged PRs (30d)
154

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • No Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from fable-compiler/Fable

All issues in fable-compiler/Fable

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.