Skip to content
13 changes: 11 additions & 2 deletions src/uu/numfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ fn transform_to(
round_method: RoundMethod,
precision: usize,
unit_separator: &str,
is_precision_specified: bool,
) -> Result<String> {
let i2 = s / (opts.to_unit as f64);
let (i2, s) = consider_suffix(i2, opts.to, round_method, precision)?;
Expand All @@ -502,10 +503,16 @@ fn transform_to(
DisplayableSuffix(s, opts.to),
)
}
Some(s) if is_precision_specified => {
format!("{i2:.0}{unit_separator}{}", DisplayableSuffix(s, opts.to))
}
Some(s) if i2.abs() < 10.0 => {
// when there's a single digit before the dot.
format!("{i2:.1}{unit_separator}{}", DisplayableSuffix(s, opts.to))
}
Some(s) => format!("{i2:.0}{unit_separator}{}", DisplayableSuffix(s, opts.to)),
Some(s) => {
format!("{i2:.0}{unit_separator}{}", DisplayableSuffix(s, opts.to))
}
})
}

Expand Down Expand Up @@ -539,7 +546,7 @@ fn format_string(
Some(suffix) => source.strip_suffix(suffix).unwrap_or(source),
None => source,
};

let mut is_precision_specified = true;
let precision = if let Some(p) = options.format.precision {
p
} else if options.transform.to == Unit::None
Expand All @@ -550,6 +557,7 @@ fn format_string(
{
parse_implicit_precision(source_without_suffix)
} else {
is_precision_specified = false;
0
};

Expand All @@ -559,6 +567,7 @@ fn format_string(
options.round,
precision,
&options.unit_separator,
is_precision_specified,
)?;

// bring back the suffix before applying padding
Expand Down
1 change: 0 additions & 1 deletion tests/by-util/test_numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,6 @@ fn test_to_unit_prefix_selection() {
// `--format='%.0f'` with `--to=<scale>` still prints one fractional digit;
// the precision specifier `.0` is ignored.
#[test]
#[ignore = "GNU compat: see uutils/coreutils#11667"]
fn test_format_precision_zero_with_to_scale_issue_11667() {
new_ucmd!()
.args(&["--to=iec", "--format=%.0f", "5183776"])
Expand Down
Loading