numfmt: don't round floats if --from is "none"

This commit is contained in:
Daniel Hofstetter 2022-07-24 14:06:42 +02:00
parent 62305e67d1
commit 34b4853890
3 changed files with 19 additions and 1 deletions

View file

@ -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.

View file

@ -17,6 +17,7 @@ pub const IEC_BASES: [f64; 10] = [
pub type WithI = bool;
#[derive(PartialEq)]
pub enum Unit {
Auto,
Si,

View file

@ -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!()