Repository navigation
fix: apply precision before round for decimal units - #404
Open
rahul05ranjan wants to merge 1 commit into
Open
rahul05ranjan wants to merge 1 commit into
rahul05ranjan wants to merge 1 commit into
Conversation
The README documents that precision overrides round, but round ran first: 999.499 kB became 999.5, toPrecision(3) produced "1.00e+3" and the unit bumped to MB; digits beyond round were lost before toPrecision() could keep them (2.887 -> 2.89 -> "2.890"). Decimal units now feed the un-rounded scaled value to the precision pass; whole bytes still round to integers, so the sub-1-byte outputs are unchanged. Fixes avoidwork#399 Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
This branch has not been deployed
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.
Fixes #399.
The README documents that
precision"takes precedence overround", butapplyRounding()ran unconditionally beforeapplyPrecisionHandling(), so:roundwere lost beforetoPrecision()could keep them —filesize(2887, { precision: 4 })printed2.890 kBinstead of2.887 kB;filesize(999499, { precision: 3 }): 999.499 pre-rounded to 999.5,toPrecision(3)then produced1.00e+3, and the unit bumped to1.00 MBinstead of staying999 kB;roundleaked into the result even withprecisionset —filesize(1245, { precision: 2, round: 0 })printed1.0 kBinstead of1.2 kB.The change: for decimal units (
e > 0) withprecision > 0, the un-rounded scaled value goes straight to the precision pass. Whole bytes (e === 0) still round to integers — a byte count is an integer quantity, and the sub-1-byte outputs (0 B,0.0 B,1 B) are unchanged.Verification (
node --test tests/unit/, no install needed — the tests importsrc/directly):999 kB/2.887 kB/1.2 kBassertions) and passes with the fix.