Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/uu/numfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions tests/by-util/test_numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,27 @@ 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=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!()
Expand Down
Loading