Skip to content

Commit 7bbe39a

Browse files
Merge pull request #122 from TheRustyPickle/rounding-error
Rounding error
2 parents 85e93d6 + 7cd754b commit 7bbe39a

4 files changed

Lines changed: 32 additions & 111 deletions

File tree

app/src/utils.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,20 @@ pub fn parse_amount_nature_cent(amount: &str) -> Result<Option<AmountNature>> {
5858
}
5959

6060
pub fn compare_change(current: Dollar, previous: Dollar) -> String {
61-
if previous == 0.0 {
62-
"∞".to_string()
63-
} else {
64-
let diff = ((current - previous) / previous) * 100.0;
65-
if diff < 0.0 {
66-
format!("↓{:.2}", diff.abs())
67-
} else {
68-
format!("↑{diff:.2}")
69-
}
61+
match current.cent().percent_change(previous.cent()) {
62+
None => "∞".to_string(),
63+
Some(diff) if diff < 0.0 => format!("↓{:.2}", diff.abs()),
64+
Some(diff) => format!("↑{diff:.2}"),
7065
}
7166
}
7267

7368
pub fn compare_change_opt(current: Dollar, previous: Option<Dollar>) -> String {
7469
match previous {
75-
Some(prev) if prev != 0.0 => {
76-
let diff = ((current - prev) / prev) * 100.0;
77-
if diff < 0.0 {
78-
format!("↓{:.2}", diff.abs())
79-
} else {
80-
format!("↑{diff:.2}")
81-
}
82-
}
83-
_ => "∞".to_string(),
70+
None => "∞".to_string(),
71+
Some(prev) => match current.cent().percent_change(prev.cent()) {
72+
None => "∞".to_string(),
73+
Some(diff) if diff < 0.0 => format!("↓{:.2}", diff.abs()),
74+
Some(diff) => format!("↑{diff:.2}"),
75+
},
8476
}
8577
}

app/src/views/summary_models.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl PeakMonthlyMovement {
2424
}
2525
}
2626

27-
#[derive(PartialEq, Debug)]
27+
#[derive(Debug)]
2828
pub(crate) struct SummaryNet {
2929
pub(crate) total_income: Dollar,
3030
pub(crate) total_expense: Dollar,
@@ -96,13 +96,13 @@ impl SummaryNet {
9696
}
9797
}
9898

99-
#[derive(PartialEq, Debug)]
99+
#[derive(Debug)]
100100
pub(crate) enum LargestType {
101101
Earning,
102102
Expense,
103103
}
104104

105-
#[derive(PartialEq, Debug)]
105+
#[derive(Debug)]
106106
pub(crate) enum PeakType {
107107
Earning,
108108
Expense,
@@ -126,7 +126,7 @@ impl fmt::Display for PeakType {
126126
}
127127
}
128128

129-
#[derive(PartialEq, Debug)]
129+
#[derive(Debug)]
130130
pub(crate) struct SummaryLargest {
131131
largest_type: LargestType,
132132
method: String,
@@ -171,7 +171,7 @@ impl SummaryLargest {
171171
}
172172
}
173173

174-
#[derive(PartialEq, Debug)]
174+
#[derive(Debug)]
175175
pub(crate) struct SummaryPeak {
176176
peak_type: PeakType,
177177
amount: Dollar,
@@ -207,7 +207,7 @@ impl SummaryPeak {
207207
}
208208
}
209209

210-
#[derive(Debug, PartialEq)]
210+
#[derive(Debug)]
211211
pub(crate) struct SummaryMethods {
212212
method: String,
213213
pub(crate) total_earning: Dollar,
@@ -281,7 +281,7 @@ impl SummaryMethods {
281281
}
282282
}
283283

284-
#[derive(Debug, PartialEq)]
284+
#[derive(Debug)]
285285
pub(crate) struct SummaryLendBorrows {
286286
pub(crate) borrows: Dollar,
287287
pub(crate) lends: Dollar,

shared/src/models.rs

Lines changed: 14 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
66
pub 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+
922
impl 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-
2530
impl 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-
5738
impl 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-
7946
impl 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-
11770
impl 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-
155102
impl 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-
167108
impl 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-
179114
impl 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-
191120
impl 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
}

tui/src/pages/chart_ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn chart_ui(
169169
let method_at_index = tx_methods[method_index];
170170
let balance = current_balances.get(&method_at_index.id).unwrap().dollar();
171171

172-
cumulative_balance += balance;
172+
cumulative_balance += balance.value();
173173

174174
balance.value()
175175
};

0 commit comments

Comments
 (0)