summaryrefslogtreecommitdiff
path: root/amount.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2004-08-14 00:29:52 -0400
committerJohn Wiegley <johnw@newartisans.com>2004-08-14 00:29:52 -0400
commit42298cefd5ae0b4f25d4075dc19486f2782f69f1 (patch)
treee95905da36b7964c5c81a3cce2f8bacd69c5fea8 /amount.cc
parentc6b82f8359ef3b2ce5cc0dc57741f9f7c48c0e90 (diff)
downloadfork-ledger-42298cefd5ae0b4f25d4075dc19486f2782f69f1.tar.gz
fork-ledger-42298cefd5ae0b4f25d4075dc19486f2782f69f1.tar.bz2
fork-ledger-42298cefd5ae0b4f25d4075dc19486f2782f69f1.zip
optimized printing of amounts
Diffstat (limited to 'amount.cc')
-rw-r--r--amount.cc43
1 files changed, 24 insertions, 19 deletions
diff --git a/amount.cc b/amount.cc
index 2e8fc8fb..00610c05 100644
--- a/amount.cc
+++ b/amount.cc
@@ -503,32 +503,37 @@ std::ostream& operator<<(std::ostream& out, const amount_t& amt)
out << quotient;
}
else {
- bool printed = false;
+ strings_list strs;
+ char buf[4];
mpz_t temp;
mpz_init(temp);
- // jww (2003-09-29): use a smarter starting value for `powers'
- for (int powers = 15; powers >= 0; powers -= 3) {
- mpz_ui_pow_ui(divisor, 10, powers);
- mpz_tdiv_q(temp, quotient, divisor);
-
- if (mpz_sgn(temp) == 0)
- continue;
+ for (int powers = 0; true; powers += 3) {
+ if (powers > 0) {
+ mpz_ui_pow_ui(divisor, 10, powers);
+ mpz_tdiv_q(temp, quotient, divisor);
+ if (mpz_sgn(temp) == 0)
+ break;
+ mpz_tdiv_r_ui(temp, temp, 1000);
+ } else {
+ mpz_tdiv_r_ui(temp, quotient, 1000);
+ }
+ mpz_get_str(buf, 10, temp);
+ strs.push_back(buf);
+ }
- mpz_ui_pow_ui(divisor, 10, 3);
- mpz_tdiv_r(temp, temp, divisor);
+ bool printed = false;
+ for (strings_list::iterator i = strs.begin(); i != strs.end(); i++) {
if (printed) {
+ out << (amt.commodity->flags & COMMODITY_STYLE_EUROPEAN ? '.' : ',');
out.width(3);
out.fill('0');
}
- out << temp;
+ out << *i;
- if (powers > 0) {
- out << ((amt.commodity->flags & COMMODITY_STYLE_EUROPEAN) ? '.' : ',');
- printed = true;
- }
+ printed = true;
}
mpz_clear(temp);
@@ -622,10 +627,10 @@ void amount_t::parse(std::istream& in)
// [-]NUM[ ]SYM [@ AMOUNT]
// SYM[ ][-]NUM [@ AMOUNT]
- std::string symbol;
- std::string quant;
- unsigned int flags = COMMODITY_STYLE_DEFAULTS;;
- unsigned int precision = MAX_PRECISION;
+ std::string symbol;
+ std::string quant;
+ unsigned int flags = COMMODITY_STYLE_DEFAULTS;;
+ unsigned int precision = MAX_PRECISION;
INIT();