Skip to content

Commit 14491c7

Browse files
seperate round function in present_value module
1 parent e639253 commit 14491c7

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/financial/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/financial/present_value.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub fn present_value(discount_rate: f64, cash_flows: Vec<f64>) -> Result<f64, Pr
1818
.map(|(i, &cash_flow)| cash_flow / (1.0 + discount_rate).powi(i as i32))
1919
.sum::<f64>();
2020

21-
Ok((present_value * 100.0).round() / 100.0)
21+
Ok(round(present_value))
22+
}
23+
24+
fn round(value:f64)->f64{
25+
( value * 100.0).round() / 100.0
2226
}
2327

2428
#[cfg(test)]
@@ -66,4 +70,26 @@ mod tests {
6670
present_value(0.0, vec![109129.39, 30923.23, 15098.93, 29734.0, 39.0]).unwrap()
6771
);
6872
}
73+
#[test]
74+
fn test_round_function() {
75+
76+
assert_eq!(
77+
0.55,
78+
round(0.55434)
79+
);
80+
81+
assert_eq!(
82+
10.45,
83+
round(10.453)
84+
);
85+
86+
87+
assert_eq!(
88+
1111_f64,
89+
round(1111_f64)
90+
);
91+
92+
93+
}
94+
6995
}

0 commit comments

Comments
 (0)