Bitcoin ABC  0.24.7
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 <amount.h>
8 
9 #include <currencyunit.h>
10 #include <network.h>
11 #include <univalue.h>
12 #include <util/system.h>
13 
14 #include <tinyformat.h>
15 
16 static const Currency BCHA{COIN, SATOSHI, 8, "BCHA"};
17 static const Currency XEC{100 * SATOSHI, SATOSHI, 2, "XEC"};
18 
20  return gArgs.GetBoolArg("-ecash", DEFAULT_ECASH) ? XEC : BCHA;
21 }
22 
23 std::string Amount::ToString() const {
24  const auto currency = Currency::get();
25  return strprintf("%d.%0*d %s", *this / currency.baseunit, currency.decimals,
26  (*this % currency.baseunit) / currency.subunit,
27  currency.ticker);
28 }
29 
30 Amount::operator UniValue() const {
31  bool sign = *this < Amount::zero();
32  Amount n_abs(sign ? -amount : amount);
33  const auto currency = Currency::get();
34  int64_t quotient = n_abs / currency.baseunit;
35  int64_t remainder = (n_abs % currency.baseunit) / currency.subunit;
36  return UniValue(UniValue::VNUM,
37  strprintf("%s%d.%0*d", sign ? "-" : "", quotient,
38  currency.decimals, remainder));
39 }
XEC
static const Currency XEC
Definition: amount.cpp:17
Amount::ToString
std::string ToString() const
Definition: amount.cpp:23
ArgsManager::GetBoolArg
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:517
COIN
static constexpr Amount COIN
Definition: amount.h:154
UniValue
Definition: univalue.h:23
tinyformat.h
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
SATOSHI
static constexpr Amount SATOSHI
Definition: amount.h:153
univalue.h
Currency::get
static const Currency & get()
Definition: amount.cpp:19
UniValue::VNUM
@ VNUM
Definition: univalue.h:27
Amount
Definition: amount.h:19
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
currencyunit.h
gArgs
ArgsManager gArgs
Definition: system.cpp:75
network.h
DEFAULT_ECASH
constexpr bool DEFAULT_ECASH
Definition: currencyunit.h:10
Currency
Definition: amount.h:156
BCHA
static const Currency BCHA
Definition: amount.cpp:16
amount.h