From dedb6289dd79e264dc20cf672432715e6d7df460 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Mon, 24 Oct 2022 15:13:02 +0200 Subject: [PATCH] numfmt: handle negative zero values --- src/uu/numfmt/src/format.rs | 6 +++++- tests/by-util/test_numfmt.rs | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/uu/numfmt/src/format.rs b/src/uu/numfmt/src/format.rs index 3550f3bc2..5e61ce794 100644 --- a/src/uu/numfmt/src/format.rs +++ b/src/uu/numfmt/src/format.rs @@ -161,7 +161,11 @@ fn transform_from(s: &str, opts: &TransformOptions) -> Result { 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 + if n == -0.0 { + 0.0 + } else { + n + } } else if n < 0.0 { -n.abs().ceil() } else { diff --git a/tests/by-util/test_numfmt.rs b/tests/by-util/test_numfmt.rs index b12ea2d50..72a9e39a9 100644 --- a/tests/by-util/test_numfmt.rs +++ b/tests/by-util/test_numfmt.rs @@ -184,6 +184,11 @@ fn test_negative() { .stdout_is("-1.0Ki\n-1.2Mi\n-103Mi\n"); } +#[test] +fn test_negative_zero() { + new_ucmd!().pipe_in("-0\n-0.0").run().stdout_is("0\n0.0\n"); +} + #[test] fn test_no_op() { new_ucmd!()