concision_traits/
rounding.rs1use num_traits::Float;
7
8pub trait FloorDiv<Rhs = Self> {
9 type Output;
10
11 fn floor_div(self, rhs: Rhs) -> Self::Output;
12}
13
14pub trait RoundTo {
15 fn round_to(&self, places: usize) -> Self;
16}
17
18impl<T> FloorDiv for T
19where
20 T: Copy + core::ops::Div<Output = T> + core::ops::Rem<Output = T> + core::ops::Sub<Output = T>,
21{
22 type Output = T;
23
24 fn floor_div(self, rhs: Self) -> Self::Output {
25 (self - (self % rhs)) / rhs
26 }
27}
28
29impl<T> RoundTo for T
30where
31 T: Float,
32{
33 fn round_to(&self, places: usize) -> Self {
34 let factor = T::from(10).unwrap().powi(places as i32);
35 (*self * factor).round() / factor
36 }
37}