|
| 1 | +/// Format fee value in stroops with comma separators |
| 2 | +pub fn format_stroops(n: u64) -> String { |
| 3 | + let s = n.to_string(); |
| 4 | + let mut result = String::new(); |
| 5 | + for (i, c) in s.chars().rev().enumerate() { |
| 6 | + if i > 0 && i % 3 == 0 { |
| 7 | + result.push(','); |
| 8 | + } |
| 9 | + result.push(c); |
| 10 | + } |
| 11 | + result.chars().rev().collect::<String>() + " str" |
| 12 | +} |
| 13 | + |
| 14 | +/// Format fee value as XLM (1 XLM = 10,000,000 stroops) |
| 15 | +pub fn format_xlm(n: u64) -> String { |
| 16 | + let xlm = n as f64 / 10_000_000.0; |
| 17 | + format!("{:.7} XLM", xlm) |
| 18 | +} |
| 19 | + |
| 20 | +/// Auto-format: show stroops below 1M, XLM above |
| 21 | +pub fn format_fee_short(n: u64) -> String { |
| 22 | + if n < 1_000_000 { |
| 23 | + format_stroops(n) |
| 24 | + } else { |
| 25 | + format_xlm(n) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[cfg(test)] |
| 30 | +mod tests { |
| 31 | + use super::*; |
| 32 | + |
| 33 | + #[test] |
| 34 | + fn test_format_stroops() { |
| 35 | + assert_eq!(format_stroops(0), "0 str"); |
| 36 | + assert_eq!(format_stroops(100), "100 str"); |
| 37 | + assert_eq!(format_stroops(3849), "3,849 str"); |
| 38 | + assert_eq!(format_stroops(1000000), "1,000,000 str"); |
| 39 | + } |
| 40 | + |
| 41 | + #[test] |
| 42 | + fn test_format_xlm() { |
| 43 | + assert_eq!(format_xlm(0), "0.0000000 XLM"); |
| 44 | + assert_eq!(format_xlm(10_000_000), "1.0000000 XLM"); |
| 45 | + assert_eq!(format_xlm(3849), "0.0003849 XLM"); |
| 46 | + } |
| 47 | + |
| 48 | + #[test] |
| 49 | + fn test_format_fee_short() { |
| 50 | + assert_eq!(format_fee_short(3849), "3,849 str"); |
| 51 | + assert_eq!(format_fee_short(10_000_000), "1.0000000 XLM"); |
| 52 | + } |
| 53 | +} |
0 commit comments