Skip to content

Instantly share code, notes, and snippets.

@afh
Created October 11, 2011 20:56
Show Gist options
  • Select an option

  • Save afh/1279414 to your computer and use it in GitHub Desktop.

Select an option

Save afh/1279414 to your computer and use it in GitHub Desktop.
ledger floor() test
% cat <<EOF | ledger -f - --amount='(floor(amount()))' reg
D ¤ 0,000.00
01/01 foobar
Account ¤ 12.84
Other
EOF
diff --git a/src/amount.cc b/src/amount.cc
index 38b8fec..01d5fe6 100644
--- a/src/amount.cc
+++ b/src/amount.cc
@@ -115,9 +115,10 @@ namespace {
mpq_t quant,
amount_t::precision_t precision,
int zeros_prec = -1,
- mpfr_rnd_t rnd = MPFR_RNDN,
+ bool floor = false,
const optional<commodity_t&>& comm = none)
{
+ mpfr_rnd_t rnd = MPFR_RNDN;
char * buf = NULL;
try {
IF_DEBUG("amount.convert") {
@@ -150,6 +151,10 @@ namespace {
mpfr_set_prec(tempfb, num_prec + den_prec);
mpfr_div(tempfb, tempfnum, tempfden, rnd);
+ if (floor) {
+ mpfr_floor(tempfb, tempfb);
+ }
+
if (mpfr_asprintf(&buf, "%.*RNf", precision, tempfb) < 0)
throw_(amount_error,
_("Cannot output amount to a floating-point representation"));
@@ -676,7 +681,7 @@ void amount_t::in_place_floor()
_dup();
std::ostringstream out;
- stream_out_mpq(out, MP(quantity), precision_t(0), -1, MPFR_RNDZ);
+ stream_out_mpq(out, MP(quantity), precision_t(0), -1, true);
mpq_set_str(MP(quantity), out.str().c_str(), 10);
}
@@ -1259,7 +1264,7 @@ void amount_t::print(std::ostream& _out, const uint_least8_t flags) const
}
stream_out_mpq(out, MP(quantity), display_precision(),
- comm ? commodity().precision() : 0, MPFR_RNDN, comm);
+ comm ? commodity().precision() : 0, false, comm);
if (comm.has_flags(COMMODITY_STYLE_SUFFIXED)) {
if (comm.has_flags(COMMODITY_STYLE_SEPARATED))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment