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