| 17 | using util::TrimString; |
| 18 | |
| 19 | std::string FormatMoney(const CAmount n) |
| 20 | { |
| 21 | // Note: not using straight sprintf here because we do NOT want |
| 22 | // localized number formatting. |
| 23 | static_assert(COIN > 1); |
| 24 | int64_t quotient = n / COIN; |
| 25 | int64_t remainder = n % COIN; |
| 26 | if (n < 0) { |
| 27 | quotient = -quotient; |
| 28 | remainder = -remainder; |
| 29 | } |
| 30 | std::string str = strprintf("%d.%08d", quotient, remainder); |
| 31 | |
| 32 | // Right-trim excess zeros before the decimal point: |
| 33 | int nTrim = 0; |
| 34 | for (int i = str.size()-1; (str[i] == '0' && IsDigit(str[i-2])); --i) |
| 35 | ++nTrim; |
| 36 | if (nTrim) |
| 37 | str.erase(str.size()-nTrim, nTrim); |
| 38 | |
| 39 | if (n < 0) |
| 40 | str.insert(uint32_t{0}, 1, '-'); |
| 41 | return str; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | std::optional<CAmount> ParseMoney(const std::string& money_string) |