Gave round/unround/truncate all in_place_ variants

This commit is contained in:
John Wiegley 2009-02-27 02:15:27 -04:00
parent ecb1ca71c1
commit fbb0d25831
5 changed files with 89 additions and 42 deletions

View file

@ -464,32 +464,26 @@ amount_t amount_t::inverted() const
return t; return t;
} }
amount_t amount_t::rounded() const void amount_t::in_place_round()
{ {
if (! quantity) if (! quantity)
throw_(amount_error, _("Cannot set rounding for an uninitialized amount")); throw_(amount_error, _("Cannot set rounding for an uninitialized amount"));
else if (! keep_precision()) else if (! keep_precision())
return *this; return;
amount_t t(*this); _dup();
t._dup(); set_keep_precision(false);
t.set_keep_precision(false);
return t;
} }
amount_t amount_t::unrounded() const void amount_t::in_place_unround()
{ {
if (! quantity) if (! quantity)
throw_(amount_error, _("Cannot unround an uninitialized amount")); throw_(amount_error, _("Cannot unround an uninitialized amount"));
else if (keep_precision()) else if (keep_precision())
return *this; return;
amount_t t(*this); _dup();
t._dup(); set_keep_precision(true);
t.set_keep_precision(true);
return t;
} }
void amount_t::in_place_reduce() void amount_t::in_place_reduce()

View file

@ -320,17 +320,32 @@ public:
default state of an amount, but if one has become unrounded, this default state of an amount, but if one has become unrounded, this
sets the "keep precision" state back to false. sets the "keep precision" state back to false.
@see set_keep_precision */ @see set_keep_precision */
amount_t rounded() const; amount_t rounded() const {
amount_t temp(*this);
temp.in_place_round();
return temp;
}
void in_place_round();
/** Yields an amount which has lost all of its extra precision, beyond what /** Yields an amount which has lost all of its extra precision, beyond what
the display precision of the commodity would have printed. */ the display precision of the commodity would have printed. */
amount_t truncated() const { amount_t truncated() const {
return amount_t(to_string()); amount_t temp(*this);
temp.in_place_truncate();
return temp;
} }
void in_place_truncate() {
*this = amount_t(to_string());
}
/** Yields an amount whose display precision is never truncated, even /** Yields an amount whose display precision is never truncated, even
though its commodity normally displays only rounded values. */ though its commodity normally displays only rounded values. */
amount_t unrounded() const; amount_t unrounded() const {
amount_t temp(*this);
temp.in_place_unround();
return temp;
}
void in_place_unround();
/** reduces a value to its most basic commodity form, for amounts that /** reduces a value to its most basic commodity form, for amounts that
utilize "scaling commodities". For example, an amount of \c 1h utilize "scaling commodities". For example, an amount of \c 1h

View file

@ -312,24 +312,39 @@ public:
} }
balance_t rounded() const { balance_t rounded() const {
balance_t temp(*this);
temp.in_place_round();
return temp;
}
void in_place_round() {
balance_t temp; balance_t temp;
foreach (const amounts_map::value_type& pair, amounts) foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.rounded(); temp += pair.second.rounded();
return temp; *this = temp;
} }
balance_t truncated() const { balance_t truncated() const {
balance_t temp(*this);
temp.in_place_truncate();
return temp;
}
void in_place_truncate() {
balance_t temp; balance_t temp;
foreach (const amounts_map::value_type& pair, amounts) foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.truncated(); temp += pair.second.truncated();
return temp; *this = temp;
} }
balance_t unrounded() const { balance_t unrounded() const {
balance_t temp(*this);
temp.in_place_unround();
return temp;
}
void in_place_unround() {
balance_t temp; balance_t temp;
foreach (const amounts_map::value_type& pair, amounts) foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.unrounded(); temp += pair.second.unrounded();
return temp; *this = temp;
} }
balance_t reduced() const { balance_t reduced() const {

View file

@ -1280,73 +1280,79 @@ value_t value_t::abs() const
return NULL_VALUE; return NULL_VALUE;
} }
value_t value_t::rounded() const void value_t::in_place_round()
{ {
switch (type()) { switch (type()) {
case INTEGER: case INTEGER:
return *this; return;
case AMOUNT: case AMOUNT:
return as_amount().rounded(); as_amount_lval().in_place_round();
return;
case BALANCE: case BALANCE:
return as_balance().rounded(); as_balance_lval().in_place_round();
return;
case SEQUENCE: { case SEQUENCE: {
value_t temp; value_t temp;
foreach (const value_t& value, as_sequence()) foreach (const value_t& value, as_sequence())
temp.push_back(value.rounded()); temp.push_back(value.rounded());
return temp; *this = temp;
return;
} }
default: default:
break; break;
} }
throw_(value_error, _("Cannot set rounding for %1") << label()); throw_(value_error, _("Cannot set rounding for %1") << label());
return NULL_VALUE;
} }
value_t value_t::truncated() const void value_t::in_place_truncate()
{ {
switch (type()) { switch (type()) {
case INTEGER: case INTEGER:
return *this; return;
case AMOUNT: case AMOUNT:
return as_amount().truncated(); as_amount_lval().in_place_truncate();
return;
case BALANCE: case BALANCE:
return as_balance().truncated(); as_balance_lval().in_place_truncate();
return;
case SEQUENCE: { case SEQUENCE: {
value_t temp; value_t temp;
foreach (const value_t& value, as_sequence()) foreach (const value_t& value, as_sequence())
temp.push_back(value.truncated()); temp.push_back(value.truncated());
return temp; *this = temp;
return;
} }
default: default:
break; break;
} }
throw_(value_error, _("Cannot truncate %1") << label()); throw_(value_error, _("Cannot truncate %1") << label());
return NULL_VALUE;
} }
value_t value_t::unrounded() const void value_t::in_place_unround()
{ {
switch (type()) { switch (type()) {
case INTEGER: case INTEGER:
return *this; return;
case AMOUNT: case AMOUNT:
return as_amount().unrounded(); as_amount_lval().unrounded();
return;
case BALANCE: case BALANCE:
return as_balance().unrounded(); as_balance_lval().unrounded();
return;
case SEQUENCE: { case SEQUENCE: {
value_t temp; value_t temp;
foreach (const value_t& value, as_sequence()) foreach (const value_t& value, as_sequence())
temp.push_back(value.unrounded()); temp.push_back(value.unrounded());
return temp; *this = temp;
return;
} }
default: default:
break; break;
} }
throw_(value_error, _("Cannot unround %1") << label()); throw_(value_error, _("Cannot unround %1") << label());
return NULL_VALUE;
} }
void value_t::annotate(const annotation_t& details) void value_t::annotate(const annotation_t& details)

View file

@ -409,9 +409,26 @@ public:
value_t abs() const; value_t abs() const;
value_t rounded() const; value_t rounded() const {
value_t truncated() const; value_t temp(*this);
value_t unrounded() const; temp.in_place_round();
return temp;
}
void in_place_round();
value_t truncated() const {
value_t temp(*this);
temp.in_place_truncate();
return temp;
}
void in_place_truncate();
value_t unrounded() const {
value_t temp(*this);
temp.in_place_unround();
return temp;
}
void in_place_unround();
value_t reduced() const { value_t reduced() const {
value_t temp(*this); value_t temp(*this);