Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
moneystr.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2015 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <util/moneystr.h>
7
8#include <consensus/amount.h>
9#include <tinyformat.h>
10#include <util/strencodings.h>
11#include <util/string.h>
12
13std::string FormatMoney(const Amount amt) {
14 // Note: not using straight sprintf here because we do NOT want localized
15 // number formatting.
17 const auto currency = Currency::get();
18 std::string str =
19 strprintf("%d.%0*d", amt_abs / currency.baseunit, currency.decimals,
20 (amt_abs % currency.baseunit) / currency.subunit);
21
22 // Right-trim excess zeros before the decimal point:
23 int nTrim = 0;
24 for (int i = str.size() - 1; (str[i] == '0' && IsDigit(str[i - 2])); --i) {
25 ++nTrim;
26 }
27 if (nTrim) {
28 str.erase(str.size() - nTrim, nTrim);
29 }
30
31 if (amt < Amount::zero()) {
32 str.insert((unsigned int)0, 1, '-');
33 }
34 return str;
35}
36
37bool ParseMoney(const std::string &money_string, Amount &nRet) {
39 return false;
40 }
41 const std::string str = TrimString(money_string);
42 if (str.empty()) {
43 return false;
44 }
45
46 const auto &currency = Currency::get();
47 std::string strWhole;
49 const char *p = str.c_str();
50 for (; *p; p++) {
51 if (*p == '.') {
52 p++;
53 Amount nMult = currency.baseunit / 10;
54 while (IsDigit(*p) && (nMult > Amount::zero())) {
55 nUnits += (*p++ - '0') * nMult;
56 nMult /= 10;
57 }
58 break;
59 }
60 if (IsSpace(*p)) {
61 return false;
62 }
63 if (!IsDigit(*p)) {
64 return false;
65 }
66 strWhole.insert(strWhole.end(), *p);
67 }
68 if (*p) {
69 return false;
70 }
71
72 // Make sure the following overflow check is meaningful. It's fine to assert
73 // because it's on no critical path, and it's very unlikely to support a 19
74 // decimal (or more) currency anyway.
75 assert(currency.decimals <= 18);
76
77 // guard against 63 bit overflow
78 if (strWhole.size() > (size_t(18) - currency.decimals)) {
79 return false;
80 }
81 if (nUnits < Amount::zero() || nUnits > currency.baseunit) {
82 return false;
83 }
84
85 Amount nWhole = atoi64(strWhole) * currency.baseunit;
86
88 return true;
89}
std::string FormatMoney(const Amount amt)
Do not use these functions to represent or parse monetary amounts to or from JSON but use AmountFromV...
Definition moneystr.cpp:13
bool ParseMoney(const std::string &money_string, Amount &nRet)
Parse an amount denoted in full coins.
Definition moneystr.cpp:37
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
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition string.h:38
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition string.h:90
static constexpr Amount zero() noexcept
Definition amount.h:32
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...
int64_t atoi64(const std::string &str)
assert(!tx.IsCoinBase())