From 28195883da4f904f399864f4221123da7376fb2f Mon Sep 17 00:00:00 2001 From: svost Date: Thu, 13 Oct 2016 15:34:00 +0300 Subject: [PATCH] Rewrite FormatMoney function --- src/util.cpp | 17 +++++++---------- 1 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 53ad364..6a44ec8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -365,13 +365,14 @@ void ParseString(const string& str, char c, vector& v) string FormatMoney(int64_t n, bool fPlus) { - // Note: not using straight sprintf here because we do NOT want - // localized number formatting. - int64_t n_abs = (n > 0 ? n : -n); - int64_t quotient = n_abs/COIN; - int64_t remainder = n_abs%COIN; - string str = strprintf("%" PRId64 ".%06" PRId64, quotient, remainder); + ostringstream ss; + if (n < 0) + ss << '-'; + else if (fPlus && n > 0) + ss << '+'; + ss << abs(n/COIN) << '.' << setfill('0') << setw(6) << abs(n%COIN); + string str = ss.str(); // Right-trim excess zeros before the decimal point: size_t nTrim = 0; for (size_t i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i) @@ -379,10 +380,6 @@ string FormatMoney(int64_t n, bool fPlus) if (nTrim) str.erase(str.size()-nTrim, nTrim); - if (n < 0) - str.insert(0u, 1, '-'); - else if (fPlus && n > 0) - str.insert(0u, 1, '+'); return str; } -- 1.7.1