pub fn number_with_delimiter(n: i64) -> String {
let neg = n < 0;
let digits = n.unsigned_abs().to_string();
let mut out = String::new();
for (i, ch) in digits.chars().enumerate() {
if i > 0 && (digits.len() - i).is_multiple_of(3) {
out.push(',');
}
out.push(ch);
}
if neg {
format!("-{out}")
} else {
out
}
}
pub fn number_to_currency(amount: f64) -> String {
number_to_currency_unit(amount, "$")
}
pub fn number_to_currency_unit(amount: f64, unit: &str) -> String {
let rounded = (amount * 100.0).round() / 100.0;
let whole = rounded.trunc() as i64;
let cents = ((rounded.abs() - whole.unsigned_abs() as f64) * 100.0).round() as u64;
format!("{unit}{}.{:02}", number_with_delimiter(whole), cents)
}
pub fn number_to_percentage(value: f64, precision: usize) -> String {
format!("{value:.precision$}%")
}