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
23#include <xrpl/basics/safe_cast.h>
24
25#include <iomanip>
26
27namespace ripple {
28namespace test {
29namespace jtx {
30
31#if 0
34 AnyAmount const& amount)
35{
36 if (amount.is_any)
37 {
38 os << amount.value.getText() << "/" <<
39 to_string(amount.value.issue().currency) <<
40 "*";
41 return os;
42 }
43 os << amount.value.getText() << "/" <<
44 to_string(amount.value.issue().currency) <<
45 "(" << amount.name() << ")";
46 return os;
47}
48#endif
49
50PrettyAmount::operator AnyAmount() const
51{
52 return {amount_};
53}
54
55template <typename T>
56static std::string
57to_places(T const d, std::uint8_t places)
58{
59 assert(places <= std::numeric_limits<T>::digits10);
60
62 oss << std::setprecision(places) << std::fixed << d;
63
64 std::string out = oss.str();
65 out.erase(out.find_last_not_of('0') + 1, std::string::npos);
66 if (out.back() == '.')
67 out.pop_back();
68
69 return out;
70}
71
73operator<<(std::ostream& os, PrettyAmount const& amount)
74{
75 if (amount.value().native())
76 {
77 // measure in hundredths
78 auto const c = dropsPerXRP.drops() / 100;
79 auto const n = amount.value().mantissa();
80 if (n < c)
81 {
82 if (amount.value().negative())
83 os << "-" << n << " drops";
84 else
85 os << n << " drops";
86 return os;
87 }
88 auto const d = double(n) / dropsPerXRP.drops();
89 if (amount.value().negative())
90 os << "-";
91
92 os << to_places(d, 6) << " XRP";
93 }
94 else
95 {
96 os << amount.value().getText() << "/"
97 << to_string(amount.value().issue().currency) << "(" << amount.name()
98 << ")";
99 }
100 return os;
101}
102
103//------------------------------------------------------------------------------
104
105XRP_t const XRP{};
106
109{
110 return {STAmount(issue(), 1, -81), account.name()};
111}
112
115{
116 return {
117 STAmount(issue(), safe_cast<std::uint64_t>(m.n), -81), account.name()};
118}
119
121operator<<(std::ostream& os, IOU const& iou)
122{
123 os << to_string(iou.issue().currency) << "(" << iou.account.name() << ")";
124 return os;
125}
126
127any_t const any{};
128
129} // namespace jtx
130} // namespace test
131} // namespace ripple
Currency currency
Definition: Issue.h:35
std::string getText() const override
Definition: STAmount.cpp:706
bool negative() const noexcept
Definition: STAmount.h:471
Issue const & issue() const
Definition: STAmount.h:496
std::uint64_t mantissa() const noexcept
Definition: STAmount.h:477
bool native() const noexcept
Definition: STAmount.h:458
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:83
Converts to IOU Issue or STAmount.
PrettyAmount operator()(T v) const
T fixed(T... args)
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition: amount.cpp:73
constexpr XRPAmount dropsPerXRP
any_t const any
Returns an amount representing "any issuer".
Definition: amount.cpp:127
static std::string to_places(T const d, std::uint8_t places)
Definition: amount.cpp:57
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630
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