@@ -6,6 +6,19 @@ use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
66pub const LAST_POSSIBLE_TIME : NaiveTime =
77 NaiveTime :: from_hms_nano_opt ( 23 , 59 , 59 , 999_999_999 ) . unwrap ( ) ;
88
9+ impl Cent {
10+ #[ must_use]
11+ pub fn percent_change ( self , previous : Cent ) -> Option < f64 > {
12+ if previous. 0 == 0 {
13+ None
14+ } else {
15+ let cur = self . 0 as f64 ;
16+ let prev = previous. 0 as f64 ;
17+ Some ( ( ( cur - prev) / prev) * 100.0 )
18+ }
19+ }
20+ }
21+
922impl Add < i64 > for Cent {
1023 type Output = Cent ;
1124
@@ -14,14 +27,6 @@ impl Add<i64> for Cent {
1427 }
1528}
1629
17- impl Add < f64 > for Dollar {
18- type Output = Dollar ;
19-
20- fn add ( self , rhs : f64 ) -> Self :: Output {
21- Dollar ( self . 0 + rhs)
22- }
23- }
24-
2530impl Sub < i64 > for Cent {
2631 type Output = Cent ;
2732
@@ -30,30 +35,6 @@ impl Sub<i64> for Cent {
3035 }
3136}
3237
33- impl Sub < f64 > for Dollar {
34- type Output = Dollar ;
35-
36- fn sub ( self , rhs : f64 ) -> Self :: Output {
37- Dollar ( self . 0 - rhs)
38- }
39- }
40-
41- impl Sub for Dollar {
42- type Output = Dollar ;
43-
44- fn sub ( self , rhs : Dollar ) -> Self :: Output {
45- Dollar ( self . 0 - rhs. 0 )
46- }
47- }
48-
49- impl Div for Dollar {
50- type Output = f64 ;
51-
52- fn div ( self , rhs : Dollar ) -> Self :: Output {
53- self . 0 / rhs. 0
54- }
55- }
56-
5738impl Mul < i64 > for Cent {
5839 type Output = Cent ;
5940
@@ -62,20 +43,6 @@ impl Mul<i64> for Cent {
6243 }
6344}
6445
65- impl Mul < f64 > for Dollar {
66- type Output = Dollar ;
67-
68- fn mul ( self , rhs : f64 ) -> Self :: Output {
69- Dollar ( self . 0 * rhs)
70- }
71- }
72-
73- impl AddAssign < f64 > for Dollar {
74- fn add_assign ( & mut self , rhs : f64 ) {
75- self . 0 += rhs;
76- }
77- }
78-
7946impl AddAssign < i64 > for Cent {
8047 fn add_assign ( & mut self , rhs : i64 ) {
8148 self . 0 += rhs;
@@ -100,20 +67,6 @@ impl AddAssign for Cent {
10067 }
10168}
10269
103- impl AddAssign < Dollar > for f64 {
104- fn add_assign ( & mut self , other : Dollar ) {
105- * self += other. 0 ;
106- }
107- }
108-
109- impl Div < i64 > for Cent {
110- type Output = Cent ;
111-
112- fn div ( self , rhs : i64 ) -> Self :: Output {
113- Cent ( self . 0 / rhs)
114- }
115- }
116-
11770impl Div < f64 > for Dollar {
11871 type Output = Dollar ;
11972
@@ -146,48 +99,24 @@ impl PartialEq<i64> for Cent {
14699 }
147100}
148101
149- impl PartialEq < f64 > for Dollar {
150- fn eq ( & self , other : & f64 ) -> bool {
151- self . 0 == * other
152- }
153- }
154-
155102impl PartialEq for Cent {
156103 fn eq ( & self , other : & Self ) -> bool {
157104 self . 0 == other. 0
158105 }
159106}
160107
161- impl PartialEq for Dollar {
162- fn eq ( & self , other : & Self ) -> bool {
163- self . 0 == other. 0
164- }
165- }
166-
167108impl PartialOrd for Cent {
168109 fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
169110 self . 0 . partial_cmp ( & other. 0 )
170111 }
171112}
172113
173- impl PartialOrd for Dollar {
174- fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
175- self . 0 . partial_cmp ( & other. 0 )
176- }
177- }
178-
179114impl PartialOrd < i64 > for Cent {
180115 fn partial_cmp ( & self , other : & i64 ) -> Option < Ordering > {
181116 self . 0 . partial_cmp ( other)
182117 }
183118}
184119
185- impl PartialOrd < f64 > for Dollar {
186- fn partial_cmp ( & self , other : & f64 ) -> Option < Ordering > {
187- self . 0 . partial_cmp ( other)
188- }
189- }
190-
191120impl fmt:: Display for Dollar {
192121 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
193122 write ! ( f, "{:.2}" , self . 0 )
@@ -230,6 +159,6 @@ impl Dollar {
230159
231160 #[ must_use]
232161 pub fn cent ( & self ) -> Cent {
233- Cent :: new ( ( self . 0 * 100.0 ) as i64 )
162+ Cent :: new ( ( self . 0 * 100.0 ) . round ( ) as i64 )
234163 }
235164}
0 commit comments