fix: stop dropping digits when pad is combined with a locale#292
Merged
Conversation
When `pad` is used together with `locale`, the padding step inferred the
decimal separator by taking the last `.`/`,` in the already-localized
string. For a value the locale renders with grouping separators and no
decimal part, that last separator is a grouping separator, so splitting on
it dropped digit groups and skipped padding:
filesize(1234500e24, {locale: 'en', pad: true, round: 2})
// '1,234 YB' (lost '500', no padding) — expected '1,234,500.00 YB'
Let the locale formatter emit the fixed number of fraction digits via
`minimumFractionDigits`/`maximumFractionDigits`, so the localized string is
never re-parsed. The manual string padding now runs only for the non-locale
paths (plain / custom separator), where the string has a single decimal
separator. Full suite passes (171); added a regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
padis combined withlocale, the padding step infers the decimal separator by taking the last./,in the already-localized string:For a value the locale renders with grouping separators and no decimal part, that last separator is a grouping separator, so
splitproduces several parts and only the first two are used. Digit groups are dropped and padding is skipped, so the formatted string no longer represents the value:The fix lets the locale formatter emit the fixed number of fraction digits via
minimumFractionDigits/maximumFractionDigits, so the localized string is never re-parsed. The manual string padding now runs only for the non-locale paths (plain / customseparator), where the string has a single decimal separator and no grouping. The documented non-locale example (filesize(1536, {round: 3, pad: true})→1.536 kB) and the existing locale tests are unchanged.Full suite passes (171); added a regression test for the integer-with-grouping case.
(
dist/left unbuilt so the diff stays reviewable; happy to rebuild it if you prefer.)