summaryrefslogtreecommitdiff
path: root/src/amount.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2008-09-14 19:36:55 -0400
committerJohn Wiegley <johnw@newartisans.com>2008-09-14 19:36:55 -0400
commit0135c28049839c2db25351b8d8114f9f31649afc (patch)
tree6a7871175a462eacd92468b77fced92714169f0b /src/amount.cc
parent3add2229e0dd1f583596a313a76991249a521bae (diff)
downloadfork-ledger-0135c28049839c2db25351b8d8114f9f31649afc.tar.gz
fork-ledger-0135c28049839c2db25351b8d8114f9f31649afc.tar.bz2
fork-ledger-0135c28049839c2db25351b8d8114f9f31649afc.zip
Added in_place_round method to all Ledger numerical types.
Diffstat (limited to 'src/amount.cc')
-rw-r--r--src/amount.cc34
1 files changed, 17 insertions, 17 deletions
diff --git a/src/amount.cc b/src/amount.cc
index d3265bc0..8d433ad4 100644
--- a/src/amount.cc
+++ b/src/amount.cc
@@ -571,40 +571,40 @@ amount_t& amount_t::in_place_negate()
return *this;
}
-amount_t amount_t::round() const
+amount_t& amount_t::in_place_round()
{
if (! quantity)
throw_(amount_error, "Cannot round an uninitialized amount");
- if (! has_commodity())
- return *this;
+ if (has_commodity())
+ in_place_round(commodity().precision());
- return round(commodity().precision());
+ return *this;
}
-amount_t amount_t::round(precision_t prec) const
+amount_t& amount_t::in_place_round(precision_t prec)
{
if (! quantity)
throw_(amount_error, "Cannot round an uninitialized amount");
- amount_t t(*this);
-
- if (quantity->prec <= prec) {
- if (quantity && quantity->has_flags(BIGINT_KEEP_PREC)) {
- t._dup();
- t.quantity->drop_flags(BIGINT_KEEP_PREC);
+ if (quantity && quantity->prec <= prec) {
+ if (quantity->has_flags(BIGINT_KEEP_PREC)) {
+ _dup();
+ quantity->drop_flags(BIGINT_KEEP_PREC);
}
- return t;
+ return *this;
}
- t._dup();
+ DEBUG("amount.round", "Rounding " << *this << " to precision " << prec);
- mpz_round(MPZ(t.quantity), MPZ(t.quantity), t.quantity->prec, prec);
+ mpz_round(MPZ(quantity), MPZ(quantity), quantity->prec, prec);
- t.quantity->prec = prec;
- t.quantity->drop_flags(BIGINT_KEEP_PREC);
+ quantity->prec = prec;
+ quantity->drop_flags(BIGINT_KEEP_PREC);
- return t;
+ DEBUG("amount.round", " result = " << *this);
+
+ return *this;
}
amount_t amount_t::unround() const