1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! Utility functions for formatting numerical values as human-readable strings.
//!
//! This module provides helper functions for converting raw numeric values to
//! formatted strings, primarily for display purposes. It includes functions
//! for formatting large numbers with underscores as separators and for
//! converting Zcash amounts between zatoshi and ZEC representations.
/// Formats a number with underscores as thousand separators for improved readability.
///
/// This function takes a numeric value and formats it with underscores between
/// each group of three digits to improve readability of large numbers. For example,
/// `1000000` becomes `1_000_000`.
///
/// # Arguments
/// * `amount` - Any value that can be converted to u64
///
/// # Returns
/// A string with the formatted number
///
/// # Examples
/// ```
/// # use zewif::format_with_underscores;
/// #
/// assert_eq!(format_with_underscores(1000_u64), "1_000");
/// assert_eq!(format_with_underscores(1234567_u64), "1_234_567");
/// assert_eq!(format_with_underscores(1_u64), "1");
/// ```
/// Formats an amount in zatoshi (smallest Zcash unit) as a human-readable ZEC value.
///
/// This function converts a zatoshi amount to a ZEC amount with the proper decimal places.
/// There are 100,000,000 zatoshi in 1 ZEC, similar to how there are 100,000,000 satoshi
/// in 1 Bitcoin.
///
/// # Arguments
/// * `amount` - A zatoshi amount that can be converted to u64
///
/// # Returns
/// A string with the formatted ZEC amount, with the prefix "ZEC" (e.g., "ZEC 1.23456789")
///
/// # Examples
/// ```
/// # use zewif::format_zats_as_zec;
/// #
/// // 1 ZEC = 100_000_000 zatoshi
/// assert_eq!(format_zats_as_zec(100_000_000_u64), "ZEC 1.0");
///
/// // Format partial ZEC amounts
/// assert_eq!(format_zats_as_zec(123_456_789_u64), "ZEC 1.23456789");
///
/// // Trailing zeros are trimmed
/// assert_eq!(format_zats_as_zec(100_000_000_000_u64), "ZEC 1000.0");
/// ```
/// Formats a signed zatoshi amount as a human-readable ZEC value with appropriate sign.
///
/// This function extends `format_zats_as_zec` to handle negative values, which can occur
/// in certain contexts like transaction fee calculations or balance changes.
///
/// # Arguments
/// * `amount` - A signed zatoshi amount that can be converted to i64
///
/// # Returns
/// A string with the formatted ZEC amount, including sign if negative
///
/// # Examples
/// ```
/// # use zewif::format_signed_zats_as_zec;
/// #
/// // Positive values work the same as format_zats_as_zec
/// assert_eq!(format_signed_zats_as_zec(100_000_000), "ZEC 1.0");
///
/// // Negative values include a minus sign
/// assert_eq!(format_signed_zats_as_zec(-50_000_000), "-ZEC 0.5");
/// assert_eq!(format_signed_zats_as_zec(-1), "-ZEC 0.00000001");
/// ```