concision_utils/utils/arith.rs
1/*
2 Appellation: arith <utils>
3 Contrib: @FL03
4*/
5use num_traits::Float;
6
7/// divide two values and round down to the nearest integer.
8pub fn floor_div<T>(numerator: T, denom: T) -> T
9where
10 T: Copy + core::ops::Div<Output = T> + core::ops::Rem<Output = T> + core::ops::Sub<Output = T>,
11{
12 (numerator - (numerator % denom)) / denom
13}
14
15/// Round the given value to the given number of decimal places.
16pub fn round_to<T>(val: T, decimals: usize) -> T
17where
18 T: Float,
19{
20 let factor = T::from(10).expect("").powi(decimals as i32);
21 (val * factor).round() / factor
22}