Bitcoin ABC  0.24.11
P2P Digital Currency
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 <tinyformat.h>
9 #include <util/strencodings.h>
10 #include <util/string.h>
11 
12 std::string FormatMoney(const Amount amt) {
13  // Note: not using straight sprintf here because we do NOT want localized
14  // number formatting.
15  Amount amt_abs = amt > Amount::zero() ? amt : -amt;
16  const auto currency = Currency::get();
17  std::string str =
18  strprintf("%d.%0*d", amt_abs / currency.baseunit, currency.decimals,
19  (amt_abs % currency.baseunit) / currency.subunit);
20 
21  // Right-trim excess zeros before the decimal point:
22  int nTrim = 0;
23  for (int i = str.size() - 1; (str[i] == '0' && IsDigit(str[i - 2])); --i) {
24  ++nTrim;
25  }
26  if (nTrim) {
27  str.erase(str.size() - nTrim, nTrim);
28  }
29 
30  if (amt < Amount::zero()) {
31  str.insert((unsigned int)0, 1, '-');
32  }
33  return str;
34 }
35 
36 bool ParseMoney(const std::string &money_string, Amount &nRet) {
37  if (!ValidAsCString(money_string)) {
38  return false;
39  }
40  const std::string str = TrimString(money_string);
41  if (str.empty()) {
42  return false;
43  }
44 
45  const auto currency = Currency::get();
46  std::string strWhole;
47  Amount nUnits = Amount::zero();
48  const char *p = str.c_str();
49  for (; *p; p++) {
50  if (*p == '.') {
51  p++;
52  Amount nMult = currency.baseunit / 10;
53  while (IsDigit(*p) && (nMult > Amount::zero())) {
54  nUnits += (*p++ - '0') * nMult;
55  nMult /= 10;
56  }
57  break;
58  }
59  if (IsSpace(*p)) {
60  return false;
61  }
62  if (!IsDigit(*p)) {
63  return false;
64  }
65  strWhole.insert(strWhole.end(), *p);
66  }
67  if (*p) {
68  return false;
69  }
70  // guard against 63 bit overflow
71  if (strWhole.size() > 10) {
72  return false;
73  }
74  if (nUnits < Amount::zero() || nUnits > currency.baseunit) {
75  return false;
76  }
77 
78  Amount nWhole = atoi64(strWhole) * currency.baseunit;
79 
80  nRet = nWhole + Amount(nUnits);
81  return true;
82 }
atoi64
int64_t atoi64(const std::string &str)
Definition: strencodings.cpp:469
moneystr.h
string.h
TrimString
std::string TrimString(const std::string &str, const std::string &pattern=" \f\n\r\t\v")
Definition: string.h:19
ValidAsCString
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:62
IsSpace
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:102
tinyformat.h
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
strencodings.h
Currency::get
static const Currency & get()
Definition: amount.cpp:19
Amount
Definition: amount.h:19
IsDigit
constexpr bool IsDigit(char c)
Tests if the given character is a decimal digit.
Definition: strencodings.h:86
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
FormatMoney
std::string FormatMoney(const Amount amt)
Money parsing/formatting utilities.
Definition: moneystr.cpp:12
ParseMoney
bool ParseMoney(const std::string &money_string, Amount &nRet)
Parse an amount denoted in full coins.
Definition: moneystr.cpp:36