diff options
| author | Joe Carstairs <me@joeac.net> | 2026-02-28 09:10:13 +0000 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2026-02-28 09:10:13 +0000 |
| commit | 98fecb647cd672dace78f4ad3759868fb74e04f0 (patch) | |
| tree | 9d6545773d1edb4ba714ce2f029a0e235b04d1a5 /schist_desktop_gui | |
| parent | 342dadaf91093c1ce9c3ad248a2952ed3cde4de6 (diff) | |
format currency with 2 pence digits and thousands separators
Diffstat (limited to 'schist_desktop_gui')
| -rw-r--r-- | schist_desktop_gui/src/gui/components/text.rs | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/schist_desktop_gui/src/gui/components/text.rs b/schist_desktop_gui/src/gui/components/text.rs index 068e89a..30aab8d 100644 --- a/schist_desktop_gui/src/gui/components/text.rs +++ b/schist_desktop_gui/src/gui/components/text.rs @@ -79,10 +79,13 @@ impl Text { } pub fn currency(amount: i32) -> Self { + let abs_amount = amount.unsigned_abs(); + let pounds = separate_thousands(abs_amount / 100, " "); + let pence = abs_amount % 100; let formatted_string = if amount < 0 { - format!("({} · {})", -amount / 100, -amount % 100) + format!("({pounds} · {pence:02})") } else { - format!(" {} · {} ", amount / 100, amount % 100) + format!(" {pence} · {pence:02} ") }; Self::default(&formatted_string) } @@ -247,3 +250,19 @@ where } impl_focusable!(Text); + +fn separate_thousands(n: u32, separator: &str) -> String { + if n == 0 { + return String::from("0"); + } + + let mut result = String::new(); + let mut dividend = n; + while dividend > 0 { + let remainder = dividend % 1000; + result = format!("{remainder:03}{separator}{result}"); + dividend = dividend / 1000; + } + + result.trim_start_matches("0").to_string() +} |
