rippled
Loading...
Searching...
No Matches
amount.cpp
1#include <test/jtx/Account.h>
2#include <test/jtx/amount.h>
3
4#include <xrpl/basics/safe_cast.h>
5
6#include <iomanip>
7
8namespace xrpl {
9namespace test {
10namespace jtx {
11
12#if 0
15 AnyAmount const& amount)
16{
17 if (amount.is_any)
18 {
19 os << amount.value.getText() << "/" <<
20 to_string(amount.value.issue().currency) <<
21 "*";
22 return os;
23 }
24 os << amount.value.getText() << "/" <<
25 to_string(amount.value.issue().currency) <<
26 "(" << amount.name() << ")";
27 return os;
28}
29#endif
30
31PrettyAmount::
32operator AnyAmount() const
33{
34 return {amount_};
35}
36
37template <typename T>
38static std::string
39to_places(T const d, std::uint8_t places)
40{
41 assert(places <= std::numeric_limits<T>::digits10);
42
44 oss << std::setprecision(places) << std::fixed << d;
45
46 std::string out = oss.str();
47 out.erase(out.find_last_not_of('0') + 1, std::string::npos);
48 if (out.back() == '.')
49 out.pop_back();
50
51 return out;
52}
53
56{
57 if (amount.value().native())
58 {
59 // measure in hundredths
60 auto const c = dropsPerXRP.drops() / 100;
61 auto const n = amount.value().mantissa();
62 if (n < c)
63 {
64 if (amount.value().negative())
65 os << "-" << n << " drops";
66 else
67 os << n << " drops";
68 return os;
69 }
70 auto const d = double(n) / dropsPerXRP.drops();
71 if (amount.value().negative())
72 os << "-";
73
74 os << to_places(d, 6) << " XRP";
75 }
76 else if (amount.value().holds<Issue>())
77 {
78 os << amount.value().getText() << "/" << to_string(amount.value().issue().currency) << "("
79 << amount.name() << ")";
80 }
81 else
82 {
83 auto const& mptIssue = amount.value().asset().get<MPTIssue>();
84 os << amount.value().getText() << "/" << to_string(mptIssue) << "(" << amount.name() << ")";
85 }
86 return os;
87}
88
89//------------------------------------------------------------------------------
90
91XRP_t const XRP{};
92
95{
96 return {STAmount(issue(), 1, -81), account.name()};
97}
98
101{
102 return {STAmount(issue(), safe_cast<std::uint64_t>(m.n), -81), account.name()};
103}
104
106operator<<(std::ostream& os, IOU const& iou)
107{
108 os << to_string(iou.issue().currency) << "(" << iou.account.name() << ")";
109 return os;
110}
111
112any_t const any{};
113
114} // namespace jtx
115} // namespace test
116} // namespace xrpl
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:157
std::string const & name() const
Return the name.
Definition Account.h:63
Converts to IOU Issue or STAmount.
PrettyAmount operator()(T v) const
T fixed(T... args)
any_t const any
Returns an amount representing "any issuer".
Definition amount.cpp:112
static std::string to_places(T const d, std::uint8_t places)
Definition amount.cpp:39
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition amount.cpp:55
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:91
constexpr XRPAmount dropsPerXRP
auto const amount
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:600
T setprecision(T... args)
T str(T... args)
Amount specifier with an option for any issuer.
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...