From 79c90ff7040ea77f016c2256601f00ab5e935e28 Mon Sep 17 00:00:00 2001 From: Eshwar Singh Rajaputana <145380153+Eshwar1440@users.noreply.github.com> Date: Mon, 6 Apr 2026 03:58:18 -0500 Subject: [PATCH] numfmt: preserve fractional digits when --from-unit is used without suffix and fixed String format (#11674) * numfmt: preserve fractional digits when --from-unit is used without suffix and fixed String format * numfmt: simplify redundant if branches caught by clippy * numfmt: fix precision condition to only preserve decimals when input has no unit suffix --- src/uu/numfmt/src/format.rs | 10 ++++++++-- tests/by-util/test_numfmt.rs | 1 - 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index 46f7336e5..e1b5101f2 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -374,11 +374,12 @@ fn transform_from(s: &str, opts: &TransformOptions, options: &NumfmtOptions) -> .map_err(|original| { detailed_error_message(s, opts.from, &options.unit_separator).unwrap_or(original) })?; + let had_no_suffix = suffix.is_none(); let i = i * (opts.from_unit as f64); remove_suffix(i, suffix, opts.from).map(|n| { // GNU numfmt doesn't round values if no --from argument is provided by the user - if opts.from == Unit::None { + if opts.from == Unit::None || had_no_suffix { if n == -0.0 { 0.0 } else { n } } else if n < 0.0 { -n.abs().ceil() @@ -541,7 +542,12 @@ fn format_string( let precision = if let Some(p) = options.format.precision { p - } else if options.transform.from == Unit::None && options.transform.to == Unit::None { + } else if options.transform.to == Unit::None + && !source_without_suffix + .chars() + .last() + .is_some_and(char::is_alphabetic) + { parse_implicit_precision(source_without_suffix) } else { 0 diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index 18a8eab66..ae03c9ffd 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -1417,7 +1417,6 @@ fn test_to_auto_rejected_at_parse_time_issue_11662() { // `--from-unit` multiplication with fractional input rounds to an integer; // GNU preserves the fractional digits. #[test] -#[ignore = "GNU compat: see uutils/coreutils#11663"] fn test_from_unit_fractional_precision_issue_11663() { new_ucmd!() .args(&["--from=iec", "--from-unit=959", "--", "-615484.454"])