From b3c8700481eeec5875cbe33df81242f9e5eb90c3 Mon Sep 17 00:00:00 2001 From: fixfon <12512757+fixfon@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:25:20 +0300 Subject: [PATCH 1/3] numfmt: print an unsigned zero when rounding a negative number to zero 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 #15661 --- src/uu/numfmt/src/format.rs | 10 ++++++---- tests/by-util/test_numfmt.rs | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index 2196a865fb6..155e8b80b7f 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -735,10 +735,12 @@ fn transform_to( } }; Ok(match s { - None if opts.to == Unit::None && precision <= u16::MAX.into() => localize(format_float( - round_with_precision(i2, round_method, precision), - precision, - )), + None if opts.to == Unit::None && precision <= u16::MAX.into() => { + let rounded = round_with_precision(i2, round_method, precision); + // rounding a small negative number leaves -0.0, which would print as "-0" + let rounded = if rounded == 0.0 { 0.0 } else { rounded }; + localize(format_float(rounded, precision)) + } None if is_precision_specified && precision <= u16::MAX.into() => { let i2 = round_with_precision(i2, round_method, 0); localize(format_float(i2, precision)) diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 822f498180f..0ff31e64014 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -254,6 +254,18 @@ fn test_negative_zero() { .stdout_is("0\n0.0"); } +// https://github.com/uutils/coreutils/issues/15661 +// A negative number that rounds to zero prints an unsigned zero, except with `--to`. +#[rstest] +#[case::round_up_format_precision(&["--round=up", "--format=%.0f", "--", "-0.3"], "0\n")] +#[case::to_unit_round_nearest(&["--to-unit=10", "--round=nearest", "--", "-3"], "0\n")] +#[case::zero_padded(&["--round=up", "--format=%05.0f", "--", "-0.3"], "00000\n")] +#[case::fractional_precision(&["--round=nearest", "--format=%.1f", "--", "-0.04"], "0.0\n")] +#[case::to_si_keeps_signed_zero(&["--to=si", "--", "-0.4"], "-0\n")] +fn test_negative_number_rounded_to_zero(#[case] args: &[&str], #[case] expected: &str) { + new_ucmd!().args(args).succeeds().stdout_only(expected); +} + #[test] fn test_no_op() { new_ucmd!() From 1dd119a93af761d78e07de9b139b01cea757c111 Mon Sep 17 00:00:00 2001 From: fixfon <12512757+fixfon@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:38:22 +0300 Subject: [PATCH 2/3] numfmt: drop a duplicated --to=si test case --- tests/by-util/test_numfmt.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 0ff31e64014..b102c8cb235 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -255,13 +255,12 @@ fn test_negative_zero() { } // https://github.com/uutils/coreutils/issues/15661 -// A negative number that rounds to zero prints an unsigned zero, except with `--to`. +// A negative number that rounds to zero prints an unsigned zero. #[rstest] #[case::round_up_format_precision(&["--round=up", "--format=%.0f", "--", "-0.3"], "0\n")] #[case::to_unit_round_nearest(&["--to-unit=10", "--round=nearest", "--", "-3"], "0\n")] #[case::zero_padded(&["--round=up", "--format=%05.0f", "--", "-0.3"], "00000\n")] #[case::fractional_precision(&["--round=nearest", "--format=%.1f", "--", "-0.04"], "0.0\n")] -#[case::to_si_keeps_signed_zero(&["--to=si", "--", "-0.4"], "-0\n")] fn test_negative_number_rounded_to_zero(#[case] args: &[&str], #[case] expected: &str) { new_ucmd!().args(args).succeeds().stdout_only(expected); } From 66b5283a77270bfd0d58b4bcc530a0768d650ba7 Mon Sep 17 00:00:00 2001 From: fixfon <12512757+fixfon@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:25:51 +0300 Subject: [PATCH 3/3] numfmt: drop issue link comment --- tests/by-util/test_numfmt.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index b102c8cb235..036da1c09cc 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -254,7 +254,6 @@ fn test_negative_zero() { .stdout_is("0\n0.0"); } -// https://github.com/uutils/coreutils/issues/15661 // A negative number that rounds to zero prints an unsigned zero. #[rstest] #[case::round_up_format_precision(&["--round=up", "--format=%.0f", "--", "-0.3"], "0\n")]