From 90045c5e9a4aa4bf19476b043d2e1fc5b514aae1 Mon Sep 17 00:00:00 2001 From: Alexander Kireev Date: Fri, 3 Jul 2026 06:41:30 +0700 Subject: [PATCH] Fix toFormat still grouping by secondaryGroupSize when groupSize is 0 groupSize: 0 is supposed to turn grouping off entirely (per #407), but if secondaryGroupSize was also set the two get swapped internally and the check that's meant to disable grouping ends up looking at the secondary value instead. So e.g. { groupSize: 0, secondaryGroupSize: 2 } still grouped '123456789' into '1,23,45,67,89' instead of leaving it alone. Added tests covering this alongside the existing #407 cases. --- bignumber.js | 3 ++- test/methods/toFormat.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/bignumber.js b/bignumber.js index ecf03bd..017265b 100644 --- a/bignumber.js +++ b/bignumber.js @@ -2765,7 +2765,8 @@ function clone(configObject) { fractionPart = arr[1], len = intPart.length; - if (g2) { + // A groupSize of 0 disables grouping, regardless of secondaryGroupSize. + if (g1 > 0 && g2) { i = g1; g1 = g2; g2 = i; diff --git a/test/methods/toFormat.js b/test/methods/toFormat.js index 90d5d0f..0776952 100644 --- a/test/methods/toFormat.js +++ b/test/methods/toFormat.js @@ -279,6 +279,18 @@ Test('toFormat', function () { restoreDefaultFormat(); + // groupSize: 0 must disable grouping even when secondaryGroupSize is set, + // instead of falling back to grouping by secondaryGroupSize alone. + + BigNumber.config({ FORMAT: { groupSize: 0, secondaryGroupSize: 2 }}); + + t('123456789', 123456789); + t('1234.56', '1234.56'); + t('-9.91', '-9.91'); + t('1000037.123', '1000037.123456789', 3); + + restoreDefaultFormat(); + //if (typeof window == 'undefined') { // var vm = require('vm'); // t('1,234.57', '1234.567', vm.runInNewContext('[2, 2]'));