Skip to content

Commit 0f8ca82

Browse files
[JS/TS] Fix N format specifier producing trailing dot when precision is 0 (#4422)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 <[email protected]>
1 parent e3dda97 commit 0f8ca82

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
* [JS/TS] Fix `N` format specifier (`ToString("N0")`, `String.Format("{0:N0}", ...)`) producing a trailing dot when precision is 0 (fix #2582) (by @MangelMaxime)
1213
* [JS/TS] Fix `C0` and `P0` format specifiers producing trailing dot (e.g., `"¤1,000."``"¤1,000"`) (by @MangelMaxime)
1314
* [All] Fix captured side-effect-free values (e.g. empty ResizeArray) being incorrectly inlined into object expression getters in release mode, causing a new instance to be created on each getter call (fixes #3779) (by @MangelMaxime)
1415
* [Python] Fix missing `await` on else branch of ternary expressions in async closures (by @dbrattli)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
* [JS/TS] Fix `N` format specifier (`ToString("N0")`, `String.Format("{0:N0}", ...)`) producing a trailing dot when precision is 0 (fix #2582) (by @MangelMaxime)
1213
* [JS/TS] Fix `C0` and `P0` format specifiers producing trailing dot (e.g., `"¤1,000."``"¤1,000"`) (by @MangelMaxime)
1314
* [All] Fix captured side-effect-free values (e.g. empty ResizeArray) being incorrectly inlined into object expression getters in release mode, causing a new instance to be created on each getter call (fixes #3779) (by @MangelMaxime)
1415
* [Python] Fix missing `await` on else branch of ternary expressions in async closures (by @dbrattli)

src/fable-library-ts/String.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,11 @@ export function format(str: string | object, ...args: any[]) {
386386
precision = precision != null ? precision : 2;
387387
rep = toFixed(rep, precision);
388388
parts = splitIntAndDecimalPart(rep);
389-
rep = thousandSeparate(parts.integral) + "." + padRight(parts.decimal, precision, "0");
389+
if (precision > 0) {
390+
rep = thousandSeparate(parts.integral) + "." + padRight(parts.decimal, precision, "0");
391+
} else {
392+
rep = thousandSeparate(parts.integral);
393+
}
390394
break;
391395
case "p": case "P":
392396
precision = precision != null ? precision : 2;

tests/Js/Main/StringTests.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,15 @@ let tests = testList "Strings" [
477477
String.Format(CultureInfo.InvariantCulture, "{0:#}", 12343235354M) |> equal "12343235354"
478478
String.Format(CultureInfo.InvariantCulture, "{0:0}", 12343235354M) |> equal "12343235354"
479479
480+
testCase "String.Format works with N format specifier" <| fun () -> // See #2582
481+
String.Format(CultureInfo.InvariantCulture, "{0:N0}", 1000) |> equal "1,000"
482+
String.Format(CultureInfo.InvariantCulture, "{0:N0}", 12345678) |> equal "12,345,678"
483+
String.Format(CultureInfo.InvariantCulture, "{0:N0}", -1000) |> equal "-1,000"
484+
String.Format(CultureInfo.InvariantCulture, "{0:N2}", 1000) |> equal "1,000.00"
485+
String.Format(CultureInfo.InvariantCulture, "{0:N}", 1000) |> equal "1,000.00"
486+
(1000).ToString("N0") |> equal "1,000"
487+
(1000).ToString("N2") |> equal "1,000.00"
488+
480489
testCase "String.Format trims trailing zeroes when using # placeholder" <| fun () -> // Fix #2950
481490
String.Format(CultureInfo.InvariantCulture, "{0:######,###.000####}", -6789.5688) |> equal "-6,789.5688"
482491
String.Format(CultureInfo.InvariantCulture, "{0:######,###.000####}", 6789.5688) |> equal "6,789.5688"

0 commit comments

Comments
 (0)