]> Git Repo - VerusCoin.git/blame - src/utilmoneystr.cpp
checkpoints.cpp depends on main, it can use mapBlockIndex directly
[VerusCoin.git] / src / utilmoneystr.cpp
CommitLineData
4d04492b
PK
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2014 The Bitcoin developers
3// Distributed under the MIT/X11 software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
ad49c256
WL
6#include "utilmoneystr.h"
7
8#include "core.h"
9#include "tinyformat.h"
10
11using namespace std;
12
13string FormatMoney(int64_t n, bool fPlus)
14{
15 // Note: not using straight sprintf here because we do NOT want
16 // localized number formatting.
17 int64_t n_abs = (n > 0 ? n : -n);
18 int64_t quotient = n_abs/COIN;
19 int64_t remainder = n_abs%COIN;
20 string str = strprintf("%d.%08d", quotient, remainder);
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 if (nTrim)
27 str.erase(str.size()-nTrim, nTrim);
28
29 if (n < 0)
30 str.insert((unsigned int)0, 1, '-');
31 else if (fPlus && n > 0)
32 str.insert((unsigned int)0, 1, '+');
33 return str;
34}
35
36
37bool ParseMoney(const string& str, int64_t& nRet)
38{
39 return ParseMoney(str.c_str(), nRet);
40}
41
42bool ParseMoney(const char* pszIn, int64_t& nRet)
43{
44 string strWhole;
45 int64_t nUnits = 0;
46 const char* p = pszIn;
47 while (isspace(*p))
48 p++;
49 for (; *p; p++)
50 {
51 if (*p == '.')
52 {
53 p++;
54 int64_t nMult = CENT*10;
55 while (isdigit(*p) && (nMult > 0))
56 {
57 nUnits += nMult * (*p++ - '0');
58 nMult /= 10;
59 }
60 break;
61 }
62 if (isspace(*p))
63 break;
64 if (!isdigit(*p))
65 return false;
66 strWhole.insert(strWhole.end(), *p);
67 }
68 for (; *p; p++)
69 if (!isspace(*p))
70 return false;
71 if (strWhole.size() > 10) // guard against 63 bit overflow
72 return false;
73 if (nUnits < 0 || nUnits > COIN)
74 return false;
75 int64_t nWhole = atoi64(strWhole);
76 int64_t nValue = nWhole*COIN + nUnits;
77
78 nRet = nValue;
79 return true;
80}
This page took 0.027883 seconds and 4 git commands to generate.