Skip to content

[JS/TS] Fix N format specifier producing trailing dot when precision is 0#4422

Merged
MangelMaxime merged 1 commit intomainfrom
repo-assist/fix-n-format-trailing-dot-0f69c556d43fcb02
Mar 25, 2026
Merged

[JS/TS] Fix N format specifier producing trailing dot when precision is 0#4422
MangelMaxime merged 1 commit intomainfrom
repo-assist/fix-n-format-trailing-dot-0f69c556d43fcb02

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

Fixes #2582ToString("N0") and String.Format("{0:N0}", ...) produce "1,000." (with a trailing dot) instead of "1,000".

Root Cause

In src/fable-library-ts/String.ts, the N format case always appended a decimal separator and decimal digits:

case "n": case "N":
  precision = precision != null ? precision : 2;
  rep = toFixed(rep, precision);
  parts = splitIntAndDecimalPart(rep);
  rep = thousandSeparate(parts.integral) + "." + padRight(parts.decimal, precision, "0");
  //                                        ^^^--- always appended even when precision=0
  break;

When precision = 0, toFixed(1000, 0) gives "1000", splitIntAndDecimalPart gives {integral: "1000", decimal: ""}, and the code produced "1,000." (thousands separator applied correctly but trailing dot incorrectly added).

The F format case already has the correct pattern — it guards with if (precision > 0) before adding the decimal part. The N format case lacked this guard.

Fix

Mirror the F format's guard:

case "n": case "N":
  precision = precision != null ? precision : 2;
  rep = toFixed(rep, precision);
  parts = splitIntAndDecimalPart(rep);
  if (precision > 0) {
    rep = thousandSeparate(parts.integral) + "." + padRight(parts.decimal, precision, "0");
  } else {
    rep = thousandSeparate(parts.integral);
  }
  break;

Test Plan

Added tests for N0, N2, and N (default) format specifiers via both ToString and String.Format:

  • (1000).ToString("N0")"1,000"
  • (12345678).ToString("N0")"12,345,678"
  • (-1000).ToString("N0")"-1,000"
  • (1000).ToString("N2")"1,000.00"
  • (1000).ToString("N")"1,000.00"

Note: The P (percentage) and C (currency) format cases have the same trailing-dot issue for P0/C0. Those are lower-priority since their defaults are non-zero precision, but they could be fixed in a follow-up.

🤖 Generated with Repo Assist

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f

@github-actions github-actions bot added automation Automated changes repo-assist Created by Repo Assist labels Mar 19, 2026
github-actions bot added a commit that referenced this pull request Mar 21, 2026
When precision is 0, the C (currency) and P (percentage) format
specifiers were appending a decimal separator with no decimal digits,
producing results like "¤1,000." instead of "¤1,000", and "50. %"
instead of "50 %".

Mirror the guard already used by the F (fixed-point) format specifier:
only append the decimal separator when precision > 0.

The N format specifier has the same issue and is addressed separately
in PR #4422.

Co-Authored-By: Repo Assist <[email protected]>
When using ToString("N0") or String.Format("{0:N0}", ...), the N format
specifier was producing a trailing dot (e.g. "1,000." instead of "1,000").
The bug was in String.ts where the decimal separator was always appended
regardless of precision, unlike the F format which guards with precision > 0.

Adds tests for N0, N2, and N (default) format specifiers. Fixes #2582.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@MangelMaxime MangelMaxime force-pushed the repo-assist/fix-n-format-trailing-dot-0f69c556d43fcb02 branch from 885c730 to 995d251 Compare March 25, 2026 21:35
@MangelMaxime MangelMaxime marked this pull request as ready for review March 25, 2026 21:35
@MangelMaxime MangelMaxime changed the title [Repo Assist] [JS/TS] Fix N format specifier producing trailing dot when precision is 0 [JS/TS] Fix N format specifier producing trailing dot when precision is 0 Mar 25, 2026
@MangelMaxime MangelMaxime merged commit 0f8ca82 into main Mar 25, 2026
23 checks passed
@MangelMaxime MangelMaxime deleted the repo-assist/fix-n-format-trailing-dot-0f69c556d43fcb02 branch March 25, 2026 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation Automated changes repo-assist Created by Repo Assist

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Format specifies don't seem to be fully implemented

1 participant