diff options
author | John Wiegley <johnw@newartisans.com> | 2009-02-26 00:10:08 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-02-26 00:10:08 -0400 |
commit | 74e569e220bee08d6c9eda59b5e4021748344994 (patch) | |
tree | 6976db78d273adb515db5ff9be68c29c321d7811 /src | |
parent | 247cf58bfa348ba104afe9328945979c4b154e46 (diff) | |
download | fork-ledger-74e569e220bee08d6c9eda59b5e4021748344994.tar.gz fork-ledger-74e569e220bee08d6c9eda59b5e4021748344994.tar.bz2 fork-ledger-74e569e220bee08d6c9eda59b5e4021748344994.zip |
Added a truncated() method for amounts and values
When an amount is truncated, it drops all of the extra precision and
becomes exactly the value would have seen were it printed.
Diffstat (limited to 'src')
-rw-r--r-- | src/amount.h | 6 | ||||
-rw-r--r-- | src/balance.h | 7 | ||||
-rw-r--r-- | src/value.cc | 23 | ||||
-rw-r--r-- | src/value.h | 1 |
4 files changed, 37 insertions, 0 deletions
diff --git a/src/amount.h b/src/amount.h index b6ca6d16..90f1359c 100644 --- a/src/amount.h +++ b/src/amount.h @@ -322,6 +322,12 @@ public: @see set_keep_precision */ amount_t rounded() const; + /** Yields an amount which has lost all of its extra precision, beyond what + the display precision of the commodity would have printed. */ + amount_t truncated() const { + return amount_t(to_string()); + } + /** Yields an amount whose display precision is never truncated, even though its commodity normally displays only rounded values. */ amount_t unrounded() const; diff --git a/src/balance.h b/src/balance.h index a9684796..f8e4f25c 100644 --- a/src/balance.h +++ b/src/balance.h @@ -318,6 +318,13 @@ public: return temp; } + balance_t truncated() const { + balance_t temp; + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.truncated(); + return temp; + } + balance_t unrounded() const { balance_t temp; foreach (const amounts_map::value_type& pair, amounts) diff --git a/src/value.cc b/src/value.cc index 94bcfb20..75d09441 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1296,6 +1296,29 @@ value_t value_t::rounded() const return NULL_VALUE; } +value_t value_t::truncated() const +{ + switch (type()) { + case INTEGER: + return *this; + case AMOUNT: + return as_amount().truncated(); + case BALANCE: + return as_balance().truncated(); + case SEQUENCE: { + value_t temp; + foreach (const value_t& value, as_sequence()) + temp.push_back(value.truncated()); + return temp; + } + default: + break; + } + + throw_(value_error, _("Cannot truncate %1") << label()); + return NULL_VALUE; +} + value_t value_t::unrounded() const { switch (type()) { diff --git a/src/value.h b/src/value.h index 85e4a714..4d019450 100644 --- a/src/value.h +++ b/src/value.h @@ -410,6 +410,7 @@ public: value_t abs() const; value_t rounded() const; + value_t truncated() const; value_t unrounded() const; value_t reduced() const { |