diff options
| -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() +} |
