Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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 <common/args.h>
10#include <currencyunit.h>
11#include <univalue.h>
12
13#include <tinyformat.h>
14
15static const Currency BCHA{COIN, SATOSHI, 8, "BCHA"};
16static const Currency XEC{100 * SATOSHI, SATOSHI, 2, "XEC"};
17
19 return gArgs.GetBoolArg("-ecash", DEFAULT_ECASH) ? XEC : BCHA;
20}
21
22std::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
29Amount::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;
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:143
static constexpr Amount COIN
Definition amount.h:144
ArgsManager gArgs
Definition args.cpp:38
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition args.cpp:556
constexpr bool DEFAULT_ECASH
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
static constexpr Amount zero() noexcept
Definition amount.h:32
std::string ToString() const
Definition amount.cpp:22
static const Currency & get()
Definition amount.cpp:18
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...