rippled
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 <ripple/basics/safe_cast.h>
21 #include <test/jtx/Account.h>
22 #include <test/jtx/amount.h>
23 #include <cassert>
24 #include <cmath>
25 #include <iomanip>
26 
27 namespace ripple {
28 namespace test {
29 namespace 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 
50 PrettyAmount::operator AnyAmount() const
51 {
52  return { amount_ };
53 }
54 
55 template <typename T>
56 static
58 to_places(const T d, std::uint8_t places)
59 {
60  assert(places <= std::numeric_limits<T>::digits10);
61 
63  oss << std::setprecision(places) << std::fixed << d;
64 
65  std::string out = oss.str();
66  out.erase(out.find_last_not_of('0') + 1, std::string::npos);
67  if (out.back() == '.')
68  out.pop_back();
69 
70  return out;
71 }
72 
75  PrettyAmount const& amount)
76 {
77  if (amount.value().native())
78  {
79  // measure in hundredths
80  auto const c = dropsPerXRP.drops() / 100;
81  auto const n = amount.value().mantissa();
82  if(n < c)
83  {
84  if (amount.value().negative())
85  os << "-" << n << " drops";
86  else
87  os << n << " drops";
88  return os;
89  }
90  auto const d = double(n) /
92  if (amount.value().negative())
93  os << "-";
94 
95  os << to_places(d, 6) << " XRP";
96  }
97  else
98  {
99  os <<
100  amount.value().getText() << "/" <<
101  to_string(amount.value().issue().currency) <<
102  "(" << amount.name() << ")";
103  }
104  return os;
105 }
106 
107 //------------------------------------------------------------------------------
108 
109 XRP_t const XRP {};
110 
113 {
114  return { STAmount(issue(), 1, -81),
115  account.name() };
116 }
117 
120 {
121  return { STAmount(issue(),
122  safe_cast<std::uint64_t>(m.n), -81),
123  account.name() };
124 }
125 
128  IOU const& iou)
129 {
130  os <<
131  to_string(iou.issue().currency) <<
132  "(" << iou.account.name() << ")";
133  return os;
134 }
135 
136 any_t const any { };
137 
138 } // jtx
139 } // test
140 } // ripple
ripple::test::jtx::Account::name
std::string const & name() const
Return the name.
Definition: Account.h:73
std::setprecision
T setprecision(T... args)
ripple::test::jtx::epsilon_t
Definition: amount.h:290
ripple::test::jtx::XRP
const XRP_t XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:109
ripple::test::jtx::dropsPerXRP
constexpr XRPAmount dropsPerXRP
Definition: amount.h:67
std::string
STL class.
ripple::test::jtx::any_t
Definition: amount.h:378
ripple::test::jtx::operator<<
std::ostream & operator<<(std::ostream &os, PrettyAmount const &amount)
Definition: amount.cpp:74
ripple::test::jtx::to_places
static std::string to_places(const T d, std::uint8_t places)
Definition: amount.cpp:58
ripple::test::jtx::detail::epsilon_multiple::n
std::size_t n
Definition: amount.h:284
ripple::STAmount::mantissa
std::uint64_t mantissa() const noexcept
Definition: STAmount.h:155
ripple::XRPAmount::drops
constexpr value_type drops() const
Returns the number of drops.
Definition: XRPAmount.h:185
ripple::STAmount::getText
std::string getText() const override
Definition: STAmount.cpp:492
ripple::test::jtx::AnyAmount
Amount specifier with an option for any issuer.
Definition: amount.h:386
ripple::Issue::currency
Currency currency
Definition: Issue.h:37
ripple::test::jtx::detail::epsilon_multiple
Definition: amount.h:282
ripple::test::jtx::IOU::issue
Issue issue() const
Definition: amount.h:326
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
ripple::test::jtx::IOU::account
Account account
Definition: amount.h:315
cmath
ripple::test::jtx::XRP_t
Definition: amount.h:176
ripple::test::jtx::IOU::operator()
PrettyAmount operator()(T v) const
Definition: amount.h:344
ripple::QualityDirection::out
@ out
ripple::test::jtx::any
const any_t any
Returns an amount representing "any issuer".
Definition: amount.cpp:136
std::ostream
STL class.
ripple::STAmount
Definition: STAmount.h:42
std::uint8_t
std::ostringstream
STL class.
ripple::STAmount::native
bool native() const noexcept
Definition: STAmount.h:153
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::jtx::PrettyAmount::name
std::string const & name() const
Definition: amount.h:119
ripple::test::jtx::IOU
Converts to IOU Issue or STAmount.
Definition: amount.h:312
iomanip
ripple::STAmount::issue
Issue const & issue() const
Definition: STAmount.h:156
cassert
ripple::STAmount::negative
bool negative() const noexcept
Definition: STAmount.h:154
std::fixed
T fixed(T... args)
std::ostringstream::str
T str(T... args)
ripple::test::jtx::PrettyAmount::value
STAmount const & value() const
Definition: amount.h:125
std::numeric_limits
ripple::test::jtx::PrettyAmount
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
Definition: amount.h:73