Be smarter about trimming off trailing zeroes

This commit is contained in:
John Wiegley 2009-02-27 13:58:30 -04:00
parent 5e6c95e348
commit f7f07310f4
2 changed files with 8 additions and 7 deletions

View file

@ -565,8 +565,8 @@ namespace {
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)
int zeros_prec = -1,
const optional<commodity_t&>& comm = none)
{
char * buf = NULL;
try {
@ -587,7 +587,7 @@ namespace {
DEBUG("amount.convert",
"mpfr_print = " << buf << " (precision " << prec << ")");
if (no_trailing_zeroes) {
if (zeros_prec >= 0) {
int index = std::strlen(buf);
int point = 0;
for (int i = 0; i < index; i++) {
@ -597,9 +597,9 @@ namespace {
}
}
if (point > 0) {
while (--index >= point && buf[index] == '0')
while (--index >= (point + 1 + zeros_prec) && buf[index] == '0')
buf[index] = '\0';
if (index >= point && buf[index] == '.')
if (index >= (point + zeros_prec) && buf[index] == '.')
buf[index] = '\0';
}
}
@ -1024,7 +1024,8 @@ void amount_t::print(std::ostream& _out) const
out << " ";
}
stream_out_mpq(out, MP(quantity), display_precision(), ! comm, comm);
stream_out_mpq(out, MP(quantity), display_precision(),
comm ? commodity().precision() : 0, comm);
if (comm.has_flags(COMMODITY_STYLE_SUFFIXED)) {
if (comm.has_flags(COMMODITY_STYLE_SEPARATED))

View file

@ -78,7 +78,7 @@ void CommodityTestCase::testPriceHistory()
#ifdef INTEGER_MATH
assertEqual(string("$2124.12"), amt->to_fullstring());
#else
assertEqual(string("$2124.1220"), amt->to_fullstring());
assertEqual(string("$2124.122"), amt->to_fullstring());
#endif
amt = x1.value(false, CURRENT_TIME(), euro);