use moonproof_ledger::ledger::structure::{SPECKS_PER_DUST, STARS_PER_NIGHT};
#[must_use]
pub fn format_night(stars: u128) -> String {
format_scaled(stars, STARS_PER_NIGHT)
}
#[must_use]
pub fn format_dust(specks: u128) -> String {
format_scaled(specks, SPECKS_PER_DUST)
}
fn format_scaled(raw: u128, per_unit: u128) -> String {
let whole = raw / per_unit;
let frac = raw % per_unit;
if frac == 0 {
return whole.to_string();
}
let decimals = per_unit.ilog10() as usize;
let mut frac_str = format!("{frac:0>decimals$}");
while frac_str.ends_with('0') {
frac_str.pop();
}
format!("{whole}.{frac_str}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn night_scales_by_one_million() {
assert_eq!(STARS_PER_NIGHT, 1_000_000);
assert_eq!(format_night(5_000_000_000), "5000"); assert_eq!(format_night(0), "0");
assert_eq!(format_night(1_500_000), "1.5");
assert_eq!(format_night(1), "0.000001"); assert_eq!(format_night(50_000 * 1_000_000), "50000");
}
#[test]
fn dust_scales_by_ten_to_the_fifteenth() {
assert_eq!(SPECKS_PER_DUST, 1_000_000_000_000_000);
assert_eq!(format_dust(1_000_000_000_000_000), "1");
assert_eq!(format_dust(1_500_000_000_000_000), "1.5");
assert_eq!(format_dust(0), "0");
assert_eq!(format_dust(1), "0.000000000000001"); }
}