LCOV - code coverage report
Current view: top level - src - utilmoneystr.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 39 41 95.1 %
Date: 2025-02-23 09:33:43 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2014 The Bitcoin developers
       3             : // Copyright (c) 2017-2019 The PIVX Core developers
       4             : // Distributed under the MIT software license, see the accompanying
       5             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       6             : 
       7             : #include "utilmoneystr.h"
       8             : 
       9             : #include "primitives/transaction.h"
      10             : #include "tinyformat.h"
      11             : #include "utilstrencodings.h"
      12             : 
      13             : 
      14      112128 : std::string FormatMoney(const CAmount& n, bool fPlus)
      15             : {
      16             :     // Note: not using straight sprintf here because we do NOT want
      17             :     // localized number formatting.
      18      112128 :     int64_t n_abs = (n > 0 ? n : -n);
      19      112128 :     int64_t quotient = n_abs / COIN;
      20      112128 :     int64_t remainder = n_abs % COIN;
      21      112128 :     std::string str = strprintf("%d.%08d", quotient, remainder);
      22             : 
      23             :     // Right-trim excess zeros before the decimal point:
      24      112128 :     int nTrim = 0;
      25      469793 :     for (int i = str.size() - 1; (str[i] == '0' && isdigit(str[i - 2])); --i)
      26      357665 :         ++nTrim;
      27      112128 :     if (nTrim)
      28      112083 :         str.erase(str.size() - nTrim, nTrim);
      29             : 
      30      112128 :     if (n < 0)
      31          10 :         str.insert((unsigned int)0, 1, '-');
      32      112118 :     else if (fPlus && n > 0)
      33           2 :         str.insert((unsigned int)0, 1, '+');
      34      112128 :     return str;
      35             : }
      36             : 
      37             : 
      38          17 : bool ParseMoney(const std::string& str, CAmount& nRet)
      39             : {
      40          17 :     return ParseMoney(str.c_str(), nRet);
      41             : }
      42             : 
      43          61 : bool ParseMoney(const char* pszIn, CAmount& nRet)
      44             : {
      45         122 :     std::string strWhole;
      46          61 :     int64_t nUnits = 0;
      47          61 :     const char* p = pszIn;
      48          61 :     while (isspace(*p))
      49           0 :         p++;
      50         220 :     for (; *p; p++) {
      51         212 :         if (*p == '.') {
      52          51 :             p++;
      53          51 :             int64_t nMult = CENT * 10;
      54         235 :             while (isdigit(*p) && (nMult > 0)) {
      55         184 :                 nUnits += nMult * (*p++ - '0');
      56         184 :                 nMult /= 10;
      57             :             }
      58             :             break;
      59             :         }
      60         161 :         if (isspace(*p))
      61             :             break;
      62         161 :         if (!isdigit(*p))
      63             :             return false;
      64         159 :         strWhole.insert(strWhole.end(), *p);
      65             :     }
      66          59 :     for (; *p; p++)
      67           0 :         if (!isspace(*p))
      68             :             return false;
      69          59 :     if (strWhole.size() > 10) // guard against 63 bit overflow
      70             :         return false;
      71          57 :     if (nUnits < 0 || nUnits > COIN)
      72             :         return false;
      73          57 :     int64_t nWhole = atoi64(strWhole);
      74          57 :     CAmount nValue = nWhole * COIN + nUnits;
      75             : 
      76          57 :     nRet = nValue;
      77          57 :     return true;
      78             : }

Generated by: LCOV version 1.14