From 98fecb647cd672dace78f4ad3759868fb74e04f0 Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Sat, 28 Feb 2026 09:10:13 +0000 Subject: format currency with 2 pence digits and thousands separators --- schist_desktop_gui/src/gui/components/text.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'schist_desktop_gui') 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() +} -- cgit v1.2.3