Skip to content

Commit

Permalink
numfmt: don't round floats if --from is "none"
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed Jul 24, 2022
1 parent 62305e6 commit 34b4853
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/uu/numfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,16 @@ fn transform_from(s: &str, opts: &TransformOptions) -> Result<f64> {
let (i, suffix) = parse_suffix(s)?;
let i = i * (opts.from_unit as f64);

remove_suffix(i, suffix, &opts.from).map(|n| if n < 0.0 { -n.abs().ceil() } else { n.ceil() })
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 {
n
} else if n < 0.0 {
-n.abs().ceil()
} else {
n.ceil()
}
})
}

/// Divide numerator by denominator, with rounding.
Expand Down
1 change: 1 addition & 0 deletions src/uu/numfmt/src/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub const IEC_BASES: [f64; 10] = [

pub type WithI = bool;

#[derive(PartialEq)]
pub enum Unit {
Auto,
Si,
Expand Down
8 changes: 8 additions & 0 deletions tests/by-util/test_numfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

use crate::common::util::*;

#[test]
fn test_should_not_round_floats() {
new_ucmd!()
.args(&["0.99", "1.01", "1.1", "1.22", ".1", "-0.1"])
.succeeds()
.stdout_is("0.99\n1.01\n1.1\n1.22\n0.1\n-0.1\n");
}

#[test]
fn test_from_si() {
new_ucmd!()
Expand Down

0 comments on commit 34b4853

Please sign in to comment.