diff --git a/crates/nu-command/src/commands/math/round.rs b/crates/nu-command/src/commands/math/round.rs index 33000fcbe8..0c36888d8c 100644 --- a/crates/nu-command/src/commands/math/round.rs +++ b/crates/nu-command/src/commands/math/round.rs @@ -76,9 +76,12 @@ fn round_big_int(val: BigInt) -> Value { fn round_big_decimal(val: BigDecimal, precision: i64) -> Value { if precision > 0 { - UntaggedValue::decimal(val.round(precision)).into() + UntaggedValue::decimal(val.with_scale(precision + 1).round(precision)).into() } else { - let (rounded, _) = val.round(precision).as_bigint_and_exponent(); + let (rounded, _) = val + .with_scale(precision + 1) + .round(precision) + .as_bigint_and_exponent(); UntaggedValue::int(rounded).into() } } diff --git a/crates/nu-command/tests/commands/math/mod.rs b/crates/nu-command/tests/commands/math/mod.rs index d95769174a..3bbcc3ea17 100644 --- a/crates/nu-command/tests/commands/math/mod.rs +++ b/crates/nu-command/tests/commands/math/mod.rs @@ -1,6 +1,7 @@ mod avg; mod eval; mod median; +mod round; mod sum; use nu_test_support::{nu, pipeline}; diff --git a/crates/nu-command/tests/commands/math/round.rs b/crates/nu-command/tests/commands/math/round.rs new file mode 100644 index 0000000000..2bec436a64 --- /dev/null +++ b/crates/nu-command/tests/commands/math/round.rs @@ -0,0 +1,21 @@ +use nu_test_support::nu; + +#[test] +fn can_round_very_large_numbers() { + let actual = nu!( + cwd: ".", + "echo 18.1372544780074142289927665486772012345 | math round" + ); + + assert_eq!(actual.out, "18") +} + +#[test] +fn can_round_very_large_numbers_with_precision() { + let actual = nu!( + cwd: ".", + "echo 18.13725447800741422899276654867720121457878988 | math round -p 10" + ); + + assert_eq!(actual.out, "18.137254478") +}