summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Nikulin <manikulin@gmail.com>2024-08-09 17:56:23 +0700
committerJohn Wiegley <johnw@newartisans.com>2024-08-15 07:32:04 -1000
commit6d61583a7759d2fab5aacc0c2475e4d2a7d7898f (patch)
tree437dc27440a40aeda1bb0d6daae03fd08e969a16
parent61beb977684b3c5fb32236b34c940878e24ca2bf (diff)
downloadfork-ledger-6d61583a7759d2fab5aacc0c2475e4d2a7d7898f.tar.gz
fork-ledger-6d61583a7759d2fab5aacc0c2475e4d2a7d7898f.tar.bz2
fork-ledger-6d61583a7759d2fab5aacc0c2475e4d2a7d7898f.zip
Avoid string in amount_t::in_place_truncate
Do not serialize to string just to get rounded value. `in_place_roundto` should be reliable enough now.
-rw-r--r--src/amount.cc27
1 files changed, 1 insertions, 26 deletions
diff --git a/src/amount.cc b/src/amount.cc
index 22282150..3e647481 100644
--- a/src/amount.cc
+++ b/src/amount.cc
@@ -622,40 +622,15 @@ void amount_t::in_place_round()
void amount_t::in_place_truncate()
{
-#if 1
if (! quantity)
throw_(amount_error, _("Cannot truncate an uninitialized amount"));
- _dup();
-
DEBUG("amount.truncate",
"Truncating " << *this << " to precision " << display_precision());
- std::ostringstream out;
- stream_out_mpq(out, MP(quantity), display_precision());
-
- scoped_array<char> buf(new char [out.str().length() + 1]);
- std::strcpy(buf.get(), out.str().c_str());
-
- char * q = buf.get();
- for (char * p = q; *p != '\0'; p++, q++) {
- if (*p == '.') p++;
- if (p != q) *q = *p;
- }
- *q = '\0';
-
- mpq_set_str(MP(quantity), buf.get(), 10);
-
- mpz_ui_pow_ui(temp, 10, display_precision());
- mpq_set_z(tempq, temp);
- mpq_div(MP(quantity), MP(quantity), tempq);
+ in_place_roundto(display_precision());
DEBUG("amount.truncate", "Truncated = " << *this);
-#else
- // This naive implementation is straightforward, but extremely inefficient
- // as it requires parsing the commodity too, which might be fully annotated.
- *this = amount_t(to_string());
-#endif
}
void amount_t::in_place_floor()