diff options
author | John Wiegley <johnw@newartisans.com> | 2009-02-24 15:13:21 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-02-24 15:13:21 -0400 |
commit | f13709f71afec388f5dcc00f6fb468917390927d (patch) | |
tree | a1d5305fd6407b2257e3dfc4d38d71ac99431183 | |
parent | 1ecf62ce25e01def8a0df0a7c59047148c2cd734 (diff) | |
download | fork-ledger-f13709f71afec388f5dcc00f6fb468917390927d.tar.gz fork-ledger-f13709f71afec388f5dcc00f6fb468917390927d.tar.bz2 fork-ledger-f13709f71afec388f5dcc00f6fb468917390927d.zip |
Be smarter about printing uncommoditized amounts
-rw-r--r-- | src/amount.cc | 27 | ||||
-rw-r--r-- | test/baseline/opt-amount-data.test | 2 | ||||
-rw-r--r-- | test/unit/t_amount.cc | 10 |
3 files changed, 29 insertions, 10 deletions
diff --git a/src/amount.cc b/src/amount.cc index 5d44251a..813cbac0 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -561,9 +561,11 @@ int amount_t::sign() const } namespace { - void stream_out_mpq(std::ostream& out, mpq_t quant, - amount_t::precision_t prec, - const optional<commodity_t&>& comm = none) + void stream_out_mpq(std::ostream& out, + mpq_t quant, + amount_t::precision_t prec, + bool no_trailing_zeroes = false, + const optional<commodity_t&>& comm = none) { char * buf = NULL; try { @@ -584,6 +586,23 @@ namespace { DEBUG("amount.convert", "mpfr_print = " << buf << " (precision " << prec << ")"); + if (no_trailing_zeroes) { + int index = std::strlen(buf); + int point = 0; + for (int i = 0; i < index; i++) { + if (buf[i] == '.') { + point = i; + break; + } + } + if (point > 0) { + while (--index >= point && buf[index] == '0') + buf[index] = '\0'; + if (index >= point && buf[index] == '.') + buf[index] = '\0'; + } + } + if (comm) { int integer_digits = 0; if (comm && comm->has_flags(COMMODITY_STYLE_THOUSANDS)) { @@ -1006,7 +1025,7 @@ void amount_t::print(std::ostream& _out) const out << " "; } - stream_out_mpq(out, MP(quantity), display_precision(), comm); + stream_out_mpq(out, MP(quantity), display_precision(), ! comm, comm); if (comm.has_flags(COMMODITY_STYLE_SUFFIXED)) { if (comm.has_flags(COMMODITY_STYLE_SEPARATED)) diff --git a/test/baseline/opt-amount-data.test b/test/baseline/opt-amount-data.test index 0d705705..7b88c9f3 100644 --- a/test/baseline/opt-amount-data.test +++ b/test/baseline/opt-amount-data.test @@ -4,7 +4,7 @@ reg --amount-data Assets:Investments:Vanguard:VMMXX 0.350 VMMXX @ $1.00 Income:Dividends:Vanguard:VMMXX $-0.35 >>>1 -2007-02-02 0.350 +2007-02-02 0.35 2007-02-02 -0.35 >>>2 === 0 diff --git a/test/unit/t_amount.cc b/test/unit/t_amount.cc index 7e0f53b0..5e673e7c 100644 --- a/test/unit/t_amount.cc +++ b/test/unit/t_amount.cc @@ -850,11 +850,11 @@ void AmountTestCase::testIntegerDivision() assertEqual(amount_t(0L), amount_t(0L) / x1); assertEqual(amount_t(0L), 0L / x1); assertEqual(x1, x1 / 1L); - assertEqual(string("0.008130"), (amount_t(1L) / x1).to_string()); - assertEqual(string("0.008130"), (1L / x1).to_string()); + assertEqual(string("0.00813"), (amount_t(1L) / x1).to_string()); + assertEqual(string("0.00813"), (1L / x1).to_string()); assertEqual(- x1, x1 / -1L); - assertEqual(string("-0.008130"), (amount_t(-1L) / x1).to_string()); - assertEqual(string("-0.008130"), (-1L / x1).to_string()); + assertEqual(string("-0.00813"), (amount_t(-1L) / x1).to_string()); + assertEqual(string("-0.00813"), (-1L / x1).to_string()); assertEqual(string("0.269737"), (x1 / y1).to_string()); assertEqual(string("3.707317"), (y1 / x1).to_string()); assertEqual(string("0.269737"), (x1 / 456L).to_string()); @@ -906,7 +906,7 @@ void AmountTestCase::testFractionalDivision() x1 /= amount_t("456.456"); assertEqual(string("0.000590937225286255757169884601508201951"), x1.to_string()); x1 /= 456L; - assertEqual(string("0.000001295914967733017011337466214297678193292890066687335298289595263924317558590360"), x1.to_string()); + assertEqual(string("0.00000129591496773301701133746621429767819329289006668733529828959526392431755859036"), x1.to_string()); amount_t x4("1234567891234567.89123456789"); amount_t y4("56.789"); |