crate::ix!();
pub fn format_money(n: Amount) -> String {
trace!("format_money: input = {}", n);
const_assert!(COIN > 1);
let mut quotient = n / COIN;
let mut remainder = n % COIN;
let negative = n < 0;
if negative {
quotient = -quotient;
remainder = -remainder;
}
let mut out = format!("{quotient}.{remainder:08}");
while out.ends_with('0') {
let len = out.len();
if len >= 2 && out.as_bytes()[len - 2] == b'.' {
break;
}
out.pop();
}
if negative {
out.insert(0, '-');
}
trace!("format_money: output = {}", out);
out
}
#[cfg(test)]
mod tests_format_money {
use super::*;
const ONE_BTC: Amount = COIN as Amount;
#[traced_test]
fn renders_full_coin() {
assert_eq!(format_money(ONE_BTC), "1.0");
}
#[traced_test]
fn keeps_sign_and_decimal_alignment() {
let amt = -(ONE_BTC * 3 - 2500); assert_eq!(format_money(amt), "-2.999975");
}
#[traced_test]
fn zero_is_single_zero() {
assert_eq!(format_money(0), "0.0");
}
#[traced_test]
fn trims_trailing_zeros() {
let amt = ONE_BTC * 42 + 123_450;
assert_eq!(format_money(amt), "42.0012345");
}
}