diff options
author | Craig Earls <enderw88@gmail.com> | 2013-01-30 15:35:31 -0700 |
---|---|---|
committer | Craig Earls <enderw88@gmail.com> | 2013-01-30 15:35:31 -0700 |
commit | 0df13661686dfec66aa0d5a8dd88920e1e104fbc (patch) | |
tree | cbf9270fc8fe85d0ae67c1c1f3ab8d42296c232b /src/amount.cc | |
parent | 383c341d885e1a69fc63aca2ae7db4a2ff5ff1a8 (diff) | |
download | fork-ledger-0df13661686dfec66aa0d5a8dd88920e1e104fbc.tar.gz fork-ledger-0df13661686dfec66aa0d5a8dd88920e1e104fbc.tar.bz2 fork-ledger-0df13661686dfec66aa0d5a8dd88920e1e104fbc.zip |
Bug 634 Added roundto function, optimized floor and ceiling
Fixes Bug634 by adding roundto(amount, places).
Diffstat (limited to 'src/amount.cc')
-rw-r--r-- | src/amount.cc | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/src/amount.cc b/src/amount.cc index ee03827e..51e69290 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -30,6 +30,7 @@ */ #include <system.hh> +#include <math.h> #include "amount.h" #include "commodity.h" @@ -672,31 +673,31 @@ void amount_t::in_place_truncate() void amount_t::in_place_floor() { if (! quantity) - throw_(amount_error, _("Cannot floor an uninitialized amount")); + throw_(amount_error, _("Cannot compute floor on an uninitialized amount")); _dup(); - mpz_t quot; - mpz_init(quot); - mpz_fdiv_q(quot, mpq_numref(MP(quantity)), mpq_denref(MP(quantity))); - mpq_clear(MP(quantity)); - mpq_init(MP(quantity)); - mpq_set_num(MP(quantity), quot); + mpz_fdiv_q(temp, mpq_numref(MP(quantity)), mpq_denref(MP(quantity))); + mpq_set_z(MP(quantity), temp); } void amount_t::in_place_ceiling() { if (! quantity) - throw_(amount_error, _("Cannot ceiling an uninitialized amount")); + throw_(amount_error, _("Cannot compute ceiling on an uninitialized amount")); _dup(); - mpz_t quot; - mpz_init(quot); - mpz_cdiv_q(quot, mpq_numref(MP(quantity)), mpq_denref(MP(quantity))); - mpq_clear(MP(quantity)); - mpq_init(MP(quantity)); - mpq_set_num(MP(quantity), quot); + mpz_cdiv_q(temp, mpq_numref(MP(quantity)), mpq_denref(MP(quantity))); + mpq_set_z(MP(quantity), temp); +} + +void amount_t::in_place_roundto(int places) +{ + if (! quantity) + throw_(amount_error, _("Cannot round an uninitialized amount")); + double x=ceil(mpq_get_d(MP(quantity))*pow(10, places) - 0.49999999) / pow(10, places); + mpq_set_d(MP(quantity), x); } void amount_t::in_place_unround() |