Be smarter about printing uncommoditized amounts
This commit is contained in:
parent
1ecf62ce25
commit
f13709f71a
3 changed files with 29 additions and 10 deletions
|
|
@ -561,8 +561,10 @@ int amount_t::sign() const
|
|||
}
|
||||
|
||||
namespace {
|
||||
void stream_out_mpq(std::ostream& out, mpq_t quant,
|
||||
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;
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue