Changeset View
Changeset View
Standalone View
Standalone View
src/utilmoneystr.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2015 The Bitcoin Core developers | // Copyright (c) 2009-2015 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "utilmoneystr.h" | #include "utilmoneystr.h" | ||||
#include "primitives/transaction.h" | #include "primitives/transaction.h" | ||||
#include "tinyformat.h" | #include "tinyformat.h" | ||||
#include "utilstrencodings.h" | #include "utilstrencodings.h" | ||||
std::string FormatMoney(const Amount &amt) { | std::string FormatMoney(const Amount amt) { | ||||
// Note: not using straight sprintf here because we do NOT want localized | // Note: not using straight sprintf here because we do NOT want localized | ||||
// number formatting. | // number formatting. | ||||
int64_t n = amt.GetSatoshis(); | int64_t n = amt.GetSatoshis(); | ||||
int64_t n_abs = (n > 0 ? n : -n); | int64_t n_abs = (n > 0 ? n : -n); | ||||
int64_t quotient = n_abs / COIN.GetSatoshis(); | int64_t quotient = n_abs / COIN.GetSatoshis(); | ||||
int64_t remainder = n_abs % COIN.GetSatoshis(); | int64_t remainder = n_abs % COIN.GetSatoshis(); | ||||
std::string str = strprintf("%d.%08d", quotient, remainder); | std::string str = strprintf("%d.%08d", quotient, remainder); | ||||
// Right-trim excess zeros before the decimal point: | // Right-trim excess zeros before the decimal point: | ||||
int nTrim = 0; | int nTrim = 0; | ||||
for (int i = str.size() - 1; (str[i] == '0' && isdigit(str[i - 2])); --i) | for (int i = str.size() - 1; (str[i] == '0' && isdigit(str[i - 2])); --i) { | ||||
++nTrim; | ++nTrim; | ||||
if (nTrim) str.erase(str.size() - nTrim, nTrim); | } | ||||
if (nTrim) { | |||||
str.erase(str.size() - nTrim, nTrim); | |||||
} | |||||
if (n < 0) str.insert((unsigned int)0, 1, '-'); | if (n < 0) { | ||||
str.insert((unsigned int)0, 1, '-'); | |||||
} | |||||
return str; | return str; | ||||
} | } | ||||
bool ParseMoney(const std::string &str, Amount &nRet) { | bool ParseMoney(const std::string &str, Amount &nRet) { | ||||
return ParseMoney(str.c_str(), nRet); | return ParseMoney(str.c_str(), nRet); | ||||
} | } | ||||
bool ParseMoney(const char *pszIn, Amount &nRet) { | bool ParseMoney(const char *pszIn, Amount &nRet) { | ||||
std::string strWhole; | std::string strWhole; | ||||
int64_t nUnits = 0; | int64_t nUnits = 0; | ||||
const char *p = pszIn; | const char *p = pszIn; | ||||
while (isspace(*p)) | while (isspace(*p)) { | ||||
p++; | p++; | ||||
} | |||||
for (; *p; p++) { | for (; *p; p++) { | ||||
if (*p == '.') { | if (*p == '.') { | ||||
p++; | p++; | ||||
int64_t nMult = 10 * CENT.GetSatoshis(); | int64_t nMult = 10 * CENT.GetSatoshis(); | ||||
while (isdigit(*p) && (nMult > 0)) { | while (isdigit(*p) && (nMult > 0)) { | ||||
nUnits += nMult * (*p++ - '0'); | nUnits += nMult * (*p++ - '0'); | ||||
nMult /= 10; | nMult /= 10; | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
if (isspace(*p)) break; | if (isspace(*p)) { | ||||
if (!isdigit(*p)) return false; | break; | ||||
} | |||||
if (!isdigit(*p)) { | |||||
return false; | |||||
} | |||||
strWhole.insert(strWhole.end(), *p); | strWhole.insert(strWhole.end(), *p); | ||||
} | } | ||||
for (; *p; p++) | for (; *p; p++) { | ||||
if (!isspace(*p)) return false; | if (!isspace(*p)) { | ||||
return false; | |||||
} | |||||
} | |||||
// guard against 63 bit overflow | // guard against 63 bit overflow | ||||
if (strWhole.size() > 10) return false; | if (strWhole.size() > 10) { | ||||
if (nUnits < 0 || nUnits > COIN.GetSatoshis()) return false; | return false; | ||||
} | |||||
if (nUnits < 0 || nUnits > COIN.GetSatoshis()) { | |||||
return false; | |||||
} | |||||
int64_t nWhole = atoi64(strWhole); | int64_t nWhole = atoi64(strWhole); | ||||
Amount nValue = nWhole * COIN + Amount(nUnits); | Amount nValue = nWhole * COIN + Amount(nUnits); | ||||
nRet = nValue; | nRet = nValue; | ||||
return true; | return true; | ||||
} | } |