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