Skip to content

Commit

Permalink
du: make -h the same precision as GNU coreutils
Browse files Browse the repository at this point in the history
When printing the `du -h` output GNU coreutils does not print
fractions of the size, only the full number. E.g.:
```
truncate -s12M a
du -h --apparent-size a
12M	a
```
Align our version to do the same.

Closes: uutils#6159
  • Loading branch information
mvo5 committed Apr 1, 2024
1 parent 0ef06bd commit da59cf1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ impl StatPrinter {
for &(unit, power) in &UNITS {
let limit = multiplier.pow(power);
if size >= limit {
return format!("{:.1}{}", (size as f64) / (limit as f64), unit);
return format!("{:.0}{}", (size as f64) / (limit as f64), unit);
}
}
format!("{size}B")
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,22 @@ fn test_du_h_flag_empty_file() {
.stdout_only("0\tempty.txt\n");
}

#[test]
fn test_du_h_precision() {
let (at, mut ucmd) = at_and_ucmd!();

let fpath = at.plus("12M.txt");
std::fs::File::create(&fpath)
.expect("cannot create test file")
.set_len(12 * 1024 * 1024)
.expect("cannot truncate test len to size");
ucmd.arg("-h")
.arg("--apparent-size")
.arg(&fpath)
.succeeds()
.stdout_only(format!("12M\t{}\n", &fpath.to_string_lossy()));
}

#[cfg(feature = "touch")]
#[test]
fn test_du_time() {
Expand Down

0 comments on commit da59cf1

Please sign in to comment.