2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2009-2010 Satoshi Nakamoto
|
|
|
|
|
* Copyright (C) 2009-2015 The Bitcoin Core developers
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2014-08-28 22:26:56 +02:00
|
|
|
|
2014-08-21 16:11:09 +02:00
|
|
|
#include "utilmoneystr.h"
|
|
|
|
|
|
|
|
|
|
#include "tinyformat.h"
|
2014-09-24 23:32:36 -04:00
|
|
|
#include "utilstrencodings.h"
|
2014-08-21 16:11:09 +02:00
|
|
|
|
2018-02-10 15:08:47 +01:00
|
|
|
static const CAmount COIN = 100000000;
|
|
|
|
|
static const CAmount CENT = 1000000;
|
|
|
|
|
|
2015-06-04 14:43:02 +02:00
|
|
|
std::string FormatMoney(const CAmount& n)
|
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
|
// Note: not using straight sprintf here because we do NOT want
|
|
|
|
|
// localized number formatting.
|
2017-06-17 14:01:03 -04:00
|
|
|
std::int64_t n_abs = (n > 0 ? n : -n);
|
|
|
|
|
std::int64_t quotient = n_abs/COIN;
|
|
|
|
|
std::int64_t remainder = n_abs%COIN;
|
2017-06-17 14:10:53 -04:00
|
|
|
std::string str = strprintf("%d.%08d", quotient, remainder);
|
2014-08-21 16:11:09 +02:00
|
|
|
|
|
|
|
|
// Right-trim excess zeros before the decimal point:
|
|
|
|
|
int nTrim = 0;
|
|
|
|
|
for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
|
|
|
|
|
++nTrim;
|
|
|
|
|
if (nTrim)
|
|
|
|
|
str.erase(str.size()-nTrim, nTrim);
|
|
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
|
str.insert((unsigned int)0, 1, '-');
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-06-17 14:10:53 -04:00
|
|
|
bool ParseMoney(const std::string& str, CAmount& nRet)
|
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
|
return ParseMoney(str.c_str(), nRet);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 15:46:19 -07:00
|
|
|
bool ParseMoney(const char* pszIn, CAmount& nRet)
|
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2017-06-17 14:10:53 -04:00
|
|
|
std::string strWhole;
|
2017-06-17 14:01:03 -04:00
|
|
|
std::int64_t nUnits = 0;
|
2014-08-21 16:11:09 +02:00
|
|
|
const char* p = pszIn;
|
|
|
|
|
while (isspace(*p))
|
|
|
|
|
p++;
|
|
|
|
|
for (; *p; p++)
|
|
|
|
|
{
|
|
|
|
|
if (*p == '.')
|
|
|
|
|
{
|
|
|
|
|
p++;
|
2017-06-17 14:01:03 -04:00
|
|
|
std::int64_t nMult = CENT*10;
|
2014-08-21 16:11:09 +02:00
|
|
|
while (isdigit(*p) && (nMult > 0))
|
|
|
|
|
{
|
|
|
|
|
nUnits += nMult * (*p++ - '0');
|
|
|
|
|
nMult /= 10;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (isspace(*p))
|
|
|
|
|
break;
|
|
|
|
|
if (!isdigit(*p))
|
|
|
|
|
return false;
|
|
|
|
|
strWhole.insert(strWhole.end(), *p);
|
|
|
|
|
}
|
|
|
|
|
for (; *p; p++)
|
|
|
|
|
if (!isspace(*p))
|
|
|
|
|
return false;
|
|
|
|
|
if (strWhole.size() > 10) // guard against 63 bit overflow
|
|
|
|
|
return false;
|
|
|
|
|
if (nUnits < 0 || nUnits > COIN)
|
|
|
|
|
return false;
|
2017-06-17 14:01:03 -04:00
|
|
|
std::int64_t nWhole = atoi64(strWhole);
|
2014-04-22 15:46:19 -07:00
|
|
|
CAmount nValue = nWhole*COIN + nUnits;
|
2014-08-21 16:11:09 +02:00
|
|
|
|
|
|
|
|
nRet = nValue;
|
|
|
|
|
return true;
|
|
|
|
|
}
|