1pub trait FloatWithPrecision {
2 fn round_with_precision(&self, precision: usize) -> Self;
3 fn floor_with_precision(&self, precision: usize) -> Self;
4 fn ceil_with_precision(&self, precision: usize) -> Self;
5}
6
7impl FloatWithPrecision for f64 {
8 fn round_with_precision(&self, precision: usize) -> Self {
9 let p = 10f64.powi(precision as i32);
10 (self * p).round() / p
11 }
12
13 fn floor_with_precision(&self, precision: usize) -> Self {
14 let p = 10f64.powi(precision as i32);
15 (self * p).floor() / p
16 }
17
18 fn ceil_with_precision(&self, precision: usize) -> Self {
19 let p = 10f64.powi(precision as i32);
20 (self * p).ceil() / p
21 }
22}
23
24impl FloatWithPrecision for f32 {
25 fn round_with_precision(&self, precision: usize) -> Self {
26 let p = 10f32.powi(precision as i32);
27 (self * p).round() / p
28 }
29
30 fn floor_with_precision(&self, precision: usize) -> Self {
31 let p = 10f32.powi(precision as i32);
32 (self * p).floor() / p
33 }
34
35 fn ceil_with_precision(&self, precision: usize) -> Self {
36 let p = 10f32.powi(precision as i32);
37 (self * p).ceil() / p
38 }
39}