From 8943b8096c21563e022719ce9f4712e7c2c763ce Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:43:34 +0500 Subject: [PATCH 1/2] numfmt: round a suffixed input with --round instead of always up Closes #16672 --- src/uu/numfmt/src/format.rs | 18 +++++++----------- tests/by-util/test_numfmt.rs | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index aca3cefd272..5eff46eed71 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -501,16 +501,10 @@ fn transform_from( let i = i.to_f64() * (opts.from_unit as f64); - remove_suffix(i, suffix, opts.from).map(|n| { - let n = if opts.from == Unit::None || had_no_suffix { - if n == -0.0 { 0.0 } else { n } - } else if n < 0.0 { - -n.abs().ceil() - } else { - n.ceil() - }; - ParsedNumber::Float(n) - }) + // Keep the fraction: the --round method and the output precision decide + // how it is rounded, as for a number without a suffix. + remove_suffix(i, suffix, opts.from) + .map(|n| ParsedNumber::Float(if n == -0.0 { 0.0 } else { n })) } /// Divide numerator by denominator, with rounding. @@ -771,7 +765,9 @@ fn transform_to( let i2 = round_with_precision(i2, round_method, 0); localize(format_float(i2, precision)) } - None => localize(format_float(i2, 0)), + // Below the first suffix, round the way --round says, like GNU: to a + // tenth below 10 and to a whole number above, then print no decimals. + None => localize(format_float(div_round(i2, 1.0, round_method), 0)), Some(s) if precision > 0 && precision <= u16::MAX.into() => localize(format!( "{i2:.precision$}{unit_separator}{}", DisplayableSuffix(s, opts.to), diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 69840439c7b..2fe35eb9c02 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -615,6 +615,29 @@ fn test_round_with_to_unit() { } } +#[test] +fn test_round_keeps_fraction_of_suffixed_input() { + // 0.0625K is 62.5, and is rounded like it. Without a format, only the + // suffixed number is printed as a whole number. + for (args, exp) in [ + (&["--round=down"][..], "62\n62.5\n"), + (&["--round=down", "--format=%.0f"][..], "62\n62\n"), + (&["--round=down", "--format=%.1f"][..], "62.5\n62.5\n"), + (&["--round=up", "--format=%.1f"][..], "62.5\n62.5\n"), + (&["--round=towards-zero"][..], "62\n62.5\n"), + (&["--round=up", "--to=si"][..], "63\n63\n"), + (&["--round=nearest", "--to=si"][..], "63\n63\n"), + (&["--round=down", "--to=si"][..], "62\n62\n"), + ] { + new_ucmd!() + .args(&["--from=si"]) + .args(args) + .args(&["--", "0.0625K", "62.5"]) + .succeeds() + .stdout_only(exp); + } +} + #[test] fn test_to_unit_with_unitless_small_value_uses_display_rounding() { new_ucmd!() From 219edb54a197a77caf4defe19ed4b28d8798260f Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:44:45 +0500 Subject: [PATCH 2/2] numfmt: leave rounding below the first --to suffix to #15636 --- src/uu/numfmt/src/format.rs | 4 +--- tests/by-util/test_numfmt.rs | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index 5eff46eed71..7156cd91e3f 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -765,9 +765,7 @@ fn transform_to( let i2 = round_with_precision(i2, round_method, 0); localize(format_float(i2, precision)) } - // Below the first suffix, round the way --round says, like GNU: to a - // tenth below 10 and to a whole number above, then print no decimals. - None => localize(format_float(div_round(i2, 1.0, round_method), 0)), + None => localize(format_float(i2, 0)), Some(s) if precision > 0 && precision <= u16::MAX.into() => localize(format!( "{i2:.precision$}{unit_separator}{}", DisplayableSuffix(s, opts.to), diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 2fe35eb9c02..d4e1644b2a2 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -625,8 +625,6 @@ fn test_round_keeps_fraction_of_suffixed_input() { (&["--round=down", "--format=%.1f"][..], "62.5\n62.5\n"), (&["--round=up", "--format=%.1f"][..], "62.5\n62.5\n"), (&["--round=towards-zero"][..], "62\n62.5\n"), - (&["--round=up", "--to=si"][..], "63\n63\n"), - (&["--round=nearest", "--to=si"][..], "63\n63\n"), (&["--round=down", "--to=si"][..], "62\n62\n"), ] { new_ucmd!()