rippled
Loading...
Searching...
No Matches
amount.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2015 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx/Account.h>
21#include <test/jtx/amount.h>
22#include <xrpl/basics/safe_cast.h>
23#include <cmath>
24#include <iomanip>
25
26namespace ripple {
27namespace test {
28namespace jtx {
29
30#if 0
33 AnyAmount const& amount)
34{
35 if (amount.is_any)
36 {
37 os << amount.value.getText() << "/" <<
38 to_string(amount.value.issue().currency) <<
39 "*";
40 return os;
41 }
42 os << amount.value.getText() << "/" <<
43 to_string(amount.value.issue().currency) <<
44 "(" << amount.name() << ")";
45 return os;
46}
47#endif
48
49PrettyAmount::operator AnyAmount() const
50{
51 return {amount_};
52}
53
54template <typename T>
55static std::string
56to_places(const T d, std::uint8_t places)
57{
58 assert(places <= std::numeric_limits<T>::digits10);
59
61 oss << std::setprecision(places) << std::fixed << d;
62
63 std::string out = oss.str();
64 out.erase(out.find_last_not_of('0') + 1, std::string::npos);
65 if (out.back() == '.')
66 out.pop_back();
67
68 return out;
69}
70
72operator<<(std::ostream& os, PrettyAmount const& amount)
73{
74 if (amount.value().native())
75 {
76 // measure in hundredths
77 auto const c = dropsPerXRP.drops() / 100;
78 auto const n = amount.value().mantissa();
79 if (n < c)
80 {
81 if (amount.value().negative())
82 os << "-" << n << " drops";
83 else
84 os << n << " drops";
85 return os;
86 }
87 auto const d = double(n) / dropsPerXRP.drops();
88 if (amount.value().negative())
89 os << "-";
90
91 os << to_places(d, 6) << " XRP";
92 }
93 else
94 {
95 os << amount.value().getText() << "/"
96 << to_string(amount.value().issue().currency) << "(" << amount.name()
97 << ")";
98 }
99 return os;
100}
101
102//------------------------------------------------------------------------------
103
104XRP_t const XRP{};
105
108{
109 return {STAmount(issue(), 1, -81), account.name()};
110}
111
114{
115 return {
116 STAmount(issue(), safe_cast<std::uint64_t>(m.n), -81), account.name()};
117}
118
120operator<<(std::ostream& os, IOU const& iou)
121{
122 os << to_string(iou.issue().currency) << "(" << iou.account.name() << ")";
123 return os;
124}
125
126any_t const any{};
127
128} // namespace jtx
129} // namespace test
130} // namespace ripple
Currency currency
Definition: Issue.h:38
std::string getText() const override
Definition: STAmount.cpp:515
bool negative() const noexcept
Definition: STAmount.h:462
Issue const & issue() const
Definition: STAmount.h:487
std::uint64_t mantissa() const noexcept
Definition: STAmount.h:468
bool native() const noexcept
Definition: STAmount.h:449
constexpr value_type drops() const
Returns the number of drops.
Definition: XRPAmount.h:177
std::string const & name() const
Return the name.
Definition: Account.h:82
Converts to IOU Issue or STAmount.
PrettyAmount operator()(T v) const
T fixed(T... args)
static std::string to_places(const T d, std::uint8_t places)
Definition: amount.cpp:56
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition: amount.cpp:72
constexpr XRPAmount dropsPerXRP
any_t const any
Returns an amount representing "any issuer".
Definition: amount.cpp:126
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:104
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
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 ...
STAmount const & value() const
std::string const & name() const