Skip to content

numfmt: print an unsigned zero when rounding a negative number to zero - #15682

Merged
sylvestre merged 3 commits into
uutils:mainfrom
fixfon:fix/15661-numfmt-negative-zero
Oct 24, 2026
Merged

sylvestre merged 3 commits into
uutils:mainfrom
fixfon:fix/15661-numfmt-negative-zero

Conversation

@fixfon

@fixfon fixfon commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #15661

When --round, --to-unit or a --format precision rounded a small negative number to zero, the plain (no --to) path kept the sign and printed -0, -0.0 or -0000. GNU prints an unsigned zero there. The plain path in format.rs now drops the sign when the rounded value is zero, so the four cases from the issue print 0, 0, 00000 and 0.0.

The --to path is left alone. numfmt --to=si -- -0.4 still prints -0, same as GNU, and there's already a test for it in test_numfmt_args. There are a couple of --to cases with --round=up/towards-zero that still print a signed zero; I kept those out of this PR since they go through different code.

I added the four cases from the issue to tests/by-util/test_numfmt.rs. They fail without the fix and test_numfmt passes with it. cargo fmt and clippy -D warnings are clean. Tested on macOS only.

This doesn't overlap with #15636 (that one changes consider_suffix on the --to path).

round_with_precision turns a small negative value into -0.0, and
format_float prints the sign of a negative zero, so `-0.3` with
`--round=up --format=%.0f` came out as `-0` (and `-0000` when
zero-padded). Drop the sign of a zero produced by rounding on the
plain path. The --to path keeps its existing `-0` output.

Closes uutils#15661
Comment thread tests/by-util/test_numfmt.rs Outdated
.stdout_is("0\n0.0");
}

// https://github.com/uutils/coreutils/issues/15661

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the reference to the fixed issue from the diff

@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.04 MB (-304 KB, -0.19%)

Significant per-binary changes:
  date     2.61 MB ->    2.30 MB  (-316 KB, -11.81%)

@sylvestre
sylvestre merged commit eef962b into uutils:main Oct 24, 2026
100 of 101 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

numfmt: a negative number that rounds to zero is printed as -0

3 participants