Fix negative precision round with ints (issue #9049) (#9073)

# Description
Before this PR, `math round` ignores the input if it's an `int`. This
results in the following behaviour:
```
> 123 | math round --precision -1
123
```
When the correct result is 120.

Now `int values` are converted to `float values` before actually
rounding up the number in order to take advantage of the float
implementation.

Fixes #9049.
This commit is contained in:
juanPabloMiceli
2023-05-03 18:07:32 -03:00
committed by GitHub
parent 5fcbefb7b4
commit 7fb48b9a2f
2 changed files with 45 additions and 9 deletions

View File

@ -75,11 +75,33 @@ impl Command for SubCommand {
span: Span::test_data(),
}),
},
Example {
description: "Apply negative precision to a list of numbers",
example: "[123, 123.3, -123.4] | math round -p -1",
result: Some(Value::List {
vals: vec![
Value::test_int(120),
Value::test_int(120),
Value::test_int(-120),
],
span: Span::test_data(),
}),
},
]
}
}
fn operate(value: Value, head: Span, precision: Option<i64>) -> Value {
// We treat int values as float values in order to avoid code repetition in the match closure
let value = if let Value::Int { val, span } = value {
Value::Float {
val: val as f64,
span,
}
} else {
value
};
match value {
Value::Float { val, span } => match precision {
Some(precision_number) => Value::Float {
@ -92,7 +114,6 @@ fn operate(value: Value, head: Span, precision: Option<i64>) -> Value {
span,
},
},
Value::Int { .. } => value,
Value::Error { .. } => value,
other => Value::Error {
error: Box::new(ShellError::OnlySupportsThisInputType {