Fix toFixed rounding 0 up to a power of ten at negative decimal places - #414
Merged
Merged
Conversation
round() forced the sticky/remainder flag true whenever sd < 0 (there are digits to the right of the rounding position), but an exact zero has no such digits. With ROUND_UP, ROUND_CEIL or ROUND_FLOOR this made the value 0 round away from zero, so toFixed/decimalPlaces/toFormat fabricated a power of ten: new BigNumber(0).toFixed(-2, 0) // '100' -> should be '0' new BigNumber(0).toFixed(-100, 0) // 1e100 -> should be '0' Gate the disjunct on the value being non-zero (xc[0] is 0 only for zero, the same invariant the branch below already relies on). Non-zero values are unaffected.
Owner
|
Thank you. It was Claude who handled #262 in the first place, which was probably the first time I trusted it here. My mistake. It is appropiate that it is now correcting its own work - assuming it is Claude here. |
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.
round()computes the sticky/remainder flag asr = r || sd < 0 || .... Thesd < 0disjunct means "there are discarded digits to the right of the rounding position", which is correct for a non-zero value, but an exact zero has no digits at all, so the flag must stay false. Because it is forced true,ROUND_UP/ROUND_CEIL/ROUND_FLOORround0away from zero and fabricate a power of ten.sd < 0is reached from the public API through negative decimal places (added in #262), so this surfaces intoFixed,decimalPlaces/dpandtoFormat:Rounding the exact value
0to any number of places, in any mode, is0— every reference decimal (JavaBigDecimal, Pythondecimal, .NET) agrees.Fix
Gate the disjunct on the value being non-zero:
sd < 0 && xc[0].xc[0]is0only when the value is zero — the same invariant the branch just below (if (sd < 1 || !xc[0])) already relies on.&&binds tighter than||, so the rest of the expression is unchanged, and non-zero values round exactly as before.Tests
Added zero-at-negative-dp cases to the existing ROUND_UP block and new ROUND_CEIL / ROUND_FLOOR blocks in
test/methods/toFixed.js. They fail on the current code (Expected: 0, Actual: 100) and pass with the fix. Full suite:65737 of 65737passing.dist/rebuilt vianpm run build.