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
110
111
112
113
114
115
116
117
118
119
//! Convert between human display amounts and integer base amounts.
//!
//! All value math is performed with integer arithmetic on decimal strings —
//! there is no floating-point arithmetic anywhere in this module, so
//! conversions are exact for any magnitude and never drift (e.g.
//! `to_base("0.1", 6) == "100000"`).
//!
//! QoreChain's staking coin uses a default exponent of 6 (1 QOR = 10^6 uqor),
//! but every function accepts a custom exponent for other denominations.
use crate;
/// The QoreChain staking coin's default decimal exponent (1 QOR = 10^6 uqor).
pub const DEFAULT_EXPONENT: u32 = 6;
/// Strips leading zeros from a decimal-digit string, leaving at least one digit.
/// Converts a human display amount to its integer base amount string.
///
/// `amount` is a non-negative decimal string, e.g. `"1.5"`. Surrounding
/// whitespace and a single leading `+` are tolerated. Scientific notation,
/// thousands separators, and other formatting are rejected. Returns an error if
/// `amount` is not a valid decimal string, is negative, or has more fractional
/// digits than `exponent` allows.
/// Converts an integer base amount string to a normalized display string.
///
/// `base` is a non-negative integer string, e.g. `"1500000"`. The returned
/// display amount has no trailing zeros and no trailing dot, e.g. `"1.5"`.
/// `"1000000"` becomes `"1"`, `"1"` becomes `"0.000001"`, `"0"` becomes `"0"`.
/// Returns an error if `base` is not a valid non-negative integer string.