numfmt: honor --round for an explicit --format precision of 0 - #14392
Open
Socialpranker wants to merge 1 commit into
Open
numfmt: honor --round for an explicit --format precision of 0#14392Socialpranker wants to merge 1 commit into
Socialpranker wants to merge 1 commit into
Conversation
With --to=<scale>, a "%.0f" precision took the div_round path, which keeps one fractional digit below the next base and then rounds that to nearest, so 1234567 printed 1M instead of GNU's 2M under the default from-zero rounding. An explicitly given precision now rounds the scaled value with the selected method.
|
GNU testsuite comparison: |
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.
numfmt --to=<scale> --format='%.0f'ignored--roundand always roundedthe scaled value to nearest:
--helpdocuments both halves of the contract: "Optional precision (%.1f)will override the input determined precision", and
--round=METHODwithfrom-zeroas the default. With an explicit.0,1234567 / 10^6 = 1.234567must be rounded to zero decimals by the selected method, giving2Mby default and1Munder--round=down.The fix
consider_suffixpicked its rounding byprecision > 0, so a precisionof
0fell into thediv_roundbranch. That branch is for the defaultpresentation, where a value below the next base keeps one fractional digit
(
1.3M): it rounds10 * vand divides by ten. The result,1.3, thenreached
format_float(i2, 0), which rounds to nearest — hence1Mfrom1.23, and2Mfrom1.93even with--round=down.The function now takes
is_precision_specified(already computed informat_stringand threaded totransform_to) and usesround_with_precisionwhenever the precision was given,.0included.The implicit-precision path is untouched:
consider_suffixreturns earlyfor
--to=none, which is the only case where an implicit precision can benon-zero, so the
div_roundbranch still governs every unspecified-formatinvocation.
How the GNU behavior was established
By running the installed GNU coreutils 9.11 binary (Homebrew,
gnumfmt)as a black box and diffing its output against uutils across a matrix of
--tox--roundx--formatprecisions. I did not read GNU coreutilssource.
Testing
test_format_precision_zero_with_to_scale_honors_round_methodin
tests/by-util/test_numfmt.rs: 7 cases covering the default(from-zero) plus
up,down,nearest,towards-zero, a negativevalue, and
--to=iec. Mutation-checked: reverting the one-linecondition in
format.rsmakes it fail.cargo test --features numfmt --test tests test_numfmt: 192 passed, 0failed (191 before, plus the new one).
cargo clippy -p uu_numfmt --all-targets -- -D warnings: clean.cargo fmt --check: clean.numfmtinvocations (
--to/--from/--padding/--format/--round/--suffix/--invalid/--field/--grouping): mismatches 37 -> 36. The oneremaining
--formatmismatch is a separate defect (zero-padding width),and the rest are pre-existing diagnostics wording differences. No case
regressed.
Disclosure
Prepared with AI assistance (Claude Opus 5, via Claude Code), per the AI
policy in CONTRIBUTING.md. Every GNU behavior quoted above came from
running the installed binary, not from reading GPL source. All testing was
run locally.