Skip to content

Fix toFormat grouping by secondaryGroupSize when groupSize is 0 - #411

Open
chatman-media wants to merge 1 commit into
MikeMcl:mainfrom
chatman-media:fix/toformat-groupsize-zero-secondary-group
Open

Fix toFormat grouping by secondaryGroupSize when groupSize is 0#411
chatman-media wants to merge 1 commit into
MikeMcl:mainfrom
chatman-media:fix/toformat-groupsize-zero-secondary-group

Conversation

@chatman-media

Copy link
Copy Markdown
Contributor

groupSize: 0 is supposed to disable grouping entirely (this was just fixed for the plain case in #407), but it doesn't work if secondaryGroupSize is also set. The two values get swapped internally for the Indian-style grouping logic, and the groupSize > 0 check that's meant to short-circuit grouping ends up looking at the secondary value instead.

BigNumber.config({ FORMAT: { groupSize: 0, secondaryGroupSize: 2 } });
new BigNumber(123456789).toFormat();   // '1,23,45,67,89' — should be '123456789'

Fixed by only doing the swap when the primary groupSize is actually greater than 0. Added tests next to the existing #407 cases, covering this combination.

groupSize: 0 is supposed to turn grouping off entirely (per MikeMcl#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 MikeMcl#407 cases.
@MikeMcl

MikeMcl commented Jul 5, 2026

Copy link
Copy Markdown
Owner

Well, nowhere is it stated that "groupSize: 0 is supposed to disable grouping entirely", so this is not a "fix" but a suggestion to alter the current behaviour, which is how this PR should have been framed.

The behaviour has always been that secondaryGroupSize always applies: groupSize is applied to the rightmost digits and secondaryGroupSize is applied repeatedly to the remaining digits on the left, if that is all the digits then so be it. If that grouping isn't wanted then just explicitly set secondaryGroupSize to 0, its default value.

Having said that, it may be a worthwhile change and I will look at it closely presently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants