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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use super::*;
pub trait Round {
fn round_to(self, places: i32) -> Self;
}
fn round_to(val: f32, places: i32) -> f32 {
let mult = 10_f32.powi(places);
(val * mult).round() / mult
}
impl Round for DeltaE {
fn round_to(mut self, places: i32) -> Self {
self.value = round_to(self.value, places);
self
}
}
impl Round for LabValue {
fn round_to(mut self, places: i32) -> LabValue {
self.l = round_to(self.l, places);
self.a = round_to(self.a, places);
self.b = round_to(self.b, places);
self
}
}
impl Round for LchValue {
fn round_to(mut self, places: i32) -> LchValue {
self.l = round_to(self.l, places);
self.c = round_to(self.c, places);
self.h = round_to(self.h, places);
self
}
}
impl Round for XyzValue {
fn round_to(mut self, places: i32) -> XyzValue {
self.x = round_to(self.x, places);
self.y = round_to(self.y, places);
self.z = round_to(self.z, places);
self
}
}
#[test]
fn round() {
let val = 1.234567890;
let rnd = round::round_to(val, 4);
assert_eq!(rnd, 1.2346);
assert_ne!(rnd, val);
}