From d18dec112223e821b39ae087b7d4b2550271ff44 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Mon, 7 Aug 2023 23:23:25 +0300 Subject: [PATCH] round --- ta_lib/core/src/math.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ta_lib/core/src/math.rs b/ta_lib/core/src/math.rs index b59e2e0c..c105486e 100644 --- a/ta_lib/core/src/math.rs +++ b/ta_lib/core/src/math.rs @@ -49,6 +49,11 @@ impl Series { self.fmap(|val| val.map(|v| v.abs())) } + pub fn round(&self, places: u32) -> Self { + let multiplier = 10f64.powi(places as i32); + self.fmap(|val| val.map(|v| (v * multiplier).round() / multiplier)) + } + pub fn cumsum(&self) -> Self { let mut sum = 0.0; @@ -233,6 +238,16 @@ mod tests { assert_eq!(result, expected); } + #[test] + fn test_round() { + let source = Series::from([-1.2211, 2.2456, -3.5677, 4.0, 5.3334]); + let expected = Series::from([-1.0, 2.0, -4.0, 4.0, 5.0]); + + let result = source.round(0); + + assert_eq!(result, expected); + } + #[test] fn test_cumsum() { let source = vec![1.0, 2.0, 3.0, 4.0, 5.0];