-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_float.rs
39 lines (33 loc) · 991 Bytes
/
round_float.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#![allow(clippy::let_and_return)]
use core::fmt::Debug;
use num_traits::Float;
pub trait RoundToFraction {
/// Round `float_number` to specified number of digits in the fraction.
fn round_to_fraction(&self, digits: u32) -> Self
where
Self: Float + Debug,
{
let rounded_float = if digits == 0 {
let rounded_float = self.trunc();
rounded_float
} else {
let ten = Self::from(10.0).unwrap();
let digits = Self::from(digits).unwrap();
let round_factor = ten.powf(digits);
let rounded_float = (self.mul(round_factor)).round() / round_factor;
rounded_float
};
rounded_float
}
}
impl<F> RoundToFraction for F where F: Float {}
#[cfg(test)]
mod tests {
use super::RoundToFraction;
#[test]
fn five_digits() {
let before = 100.123456789;
let after = before.round_to_fraction(5);
assert_eq!(after, 100.12346)
}
}