Bitcoin ABC  0.26.3
P2P Digital Currency
amount.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Bitcoin developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include <consensus/amount.h>
8 
9 #include <currencyunit.h>
10 #include <univalue.h>
11 #include <util/system.h>
12 
13 #include <tinyformat.h>
14 
15 static const Currency BCHA{COIN, SATOSHI, 8, "BCHA"};
16 static const Currency XEC{100 * SATOSHI, SATOSHI, 2, "XEC"};
17 
19  return gArgs.GetBoolArg("-ecash", DEFAULT_ECASH) ? XEC : BCHA;
20 }
21 
22 std::string Amount::ToString() const {
23  const auto currency = Currency::get();
24  return strprintf("%d.%0*d %s", *this / currency.baseunit, currency.decimals,
25  (*this % currency.baseunit) / currency.subunit,
26  currency.ticker);
27 }
28 
29 Amount::operator UniValue() const {
30  bool sign = *this < Amount::zero();
31  Amount n_abs(sign ? -amount : amount);
32  const auto currency = Currency::get();
33  int64_t quotient = n_abs / currency.baseunit;
34  int64_t remainder = (n_abs % currency.baseunit) / currency.subunit;
35  return UniValue(UniValue::VNUM,
36  strprintf("%s%d.%0*d", sign ? "-" : "", quotient,
37  currency.decimals, remainder));
38 }
static const Currency XEC
Definition: amount.cpp:16
static const Currency BCHA
Definition: amount.cpp:15
static constexpr Amount SATOSHI
Definition: amount.h:153
static constexpr Amount COIN
Definition: amount.h:154
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:603
@ VNUM
Definition: univalue.h:27
constexpr bool DEFAULT_ECASH
Definition: currencyunit.h:10
Definition: amount.h:19
static constexpr Amount zero()
Definition: amount.h:42
std::string ToString() const
Definition: amount.cpp:22
static const Currency & get()
Definition: amount.cpp:18
ArgsManager gArgs
Definition: system.cpp:77
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201