Skip to content

Commit f9efb9e

Browse files
Devel08sylvestre
andauthored
numfmt: don't ignore precision "0" in --format when combined with --to (#11683)
* Update format.rs * numfmt fix #11667 * numfmt --format fix * remove ignore of test for #11667 in test_numfmt.rs * Add space after slashes in a comment Co-authored-by: Sylvestre Ledru <sylvestre@debian.org> * change is_precision with a more understandable name * Leave an empty line between functions --------- Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent b352f83 commit f9efb9e

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/uu/numfmt/src/format.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ fn transform_to(
539539
round_method: RoundMethod,
540540
precision: usize,
541541
unit_separator: &str,
542+
is_precision_specified: bool,
542543
) -> Result<String> {
543544
if let Some(result) = try_format_exact_int_without_suffix_scaling(s, opts, precision) {
544545
return Ok(result);
@@ -560,10 +561,16 @@ fn transform_to(
560561
DisplayableSuffix(s, opts.to),
561562
)
562563
}
564+
Some(s) if is_precision_specified => {
565+
format!("{i2:.0}{unit_separator}{}", DisplayableSuffix(s, opts.to))
566+
}
563567
Some(s) if i2.abs() < 10.0 => {
568+
// when there's a single digit before the dot.
564569
format!("{i2:.1}{unit_separator}{}", DisplayableSuffix(s, opts.to))
565570
}
566-
Some(s) => format!("{i2:.0}{unit_separator}{}", DisplayableSuffix(s, opts.to)),
571+
Some(s) => {
572+
format!("{i2:.0}{unit_separator}{}", DisplayableSuffix(s, opts.to))
573+
}
567574
})
568575
}
569576

@@ -597,7 +604,7 @@ fn format_string(
597604
Some(suffix) => source.strip_suffix(suffix).unwrap_or(source),
598605
None => source,
599606
};
600-
607+
let mut is_precision_specified = true;
601608
let precision = if let Some(p) = options.format.precision {
602609
p
603610
} else if options.transform.to == Unit::None
@@ -608,6 +615,7 @@ fn format_string(
608615
{
609616
parse_implicit_precision(source_without_suffix)
610617
} else {
618+
is_precision_specified = false;
611619
0
612620
};
613621

@@ -617,6 +625,7 @@ fn format_string(
617625
options.round,
618626
precision,
619627
&options.unit_separator,
628+
is_precision_specified,
620629
)?;
621630

622631
// bring back the suffix before applying padding

tests/by-util/test_numfmt.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,6 @@ fn test_to_unit_prefix_selection() {
14571457
// `--format='%.0f'` with `--to=<scale>` still prints one fractional digit;
14581458
// the precision specifier `.0` is ignored.
14591459
#[test]
1460-
#[ignore = "GNU compat: see uutils/coreutils#11667"]
14611460
fn test_format_precision_zero_with_to_scale_issue_11667() {
14621461
new_ucmd!()
14631462
.args(&["--to=iec", "--format=%.0f", "5183776"])

0 commit comments

Comments
 (0)