Conversation
When a negative value rounds up to the next abbreviation boundary, the
unit was not promoted (k -> m, m -> b), so the formatter emitted
malformed output such as "-1000k" instead of "-1m".
numeral(-999999).format('0a') // was "-1000k", now "-1m"
numeral(-999999999).format('0a') // was "-1000m", now "-1b"
numeral(-999999).format('0.0a') // was "-1000.0k", now "-1.0m"
The positive mirrors already format correctly and are asserted by the
existing test data ([999999999,'0a','1b'] and [999950,'0.0a','1.0m']);
only the negative side was uncovered.
Root cause: the post-rounding promotion guard compared the signed value
(Number(int) >= 1000), so for a rounded negative like int === "-1000"
the check is false and the promotion is skipped. Comparing magnitude
(Math.abs(Number(int)) >= 1000) restores symmetry with positives.
Adds negative cases to the abbreviation test data. Full suite passes.
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.
Problem
When a negative value rounds up to the next abbreviation boundary, the formatter promotes the unit for positive numbers but not for negative ones, producing malformed output:
The positive mirrors already format correctly, and the existing test data asserts that promotion:
Only the negative side was never covered.
Root cause
The post-rounding promotion guard in
numeral._.numberToFormatcompares the signed value:For a value like
-999999, after roundingint === "-1000", soNumber(int) >= 1000isfalse(signed comparison) and the unit promotion is skipped. The number is still divided down to-1elsewhere, leaving the now-incorrectksuffix.Fix
Compare the magnitude instead, restoring symmetry with the positive path:
Verification
tests/numeral.js). They fail before the change (expected '-1000k' to equal '-1m') and pass after.src/numeral.js; the builtnumeral.jswas regenerated viagrunt build.grunt test:npm), including the existing positive abbreviation, spaced (0 a), and forced-abbreviation (0,0 ak/am/ab/at) cases.