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;
}
amount_t amount_t::rounded() const
void amount_t::in_place_round()
{
if (! quantity)
throw_(amount_error, _("Cannot set rounding for an uninitialized amount"));
else if (! keep_precision())
return *this;
return;
amount_t t(*this);
t._dup();
t.set_keep_precision(false);
return t;
_dup();
set_keep_precision(false);
}
amount_t amount_t::unrounded() const
void amount_t::in_place_unround()
{
if (! quantity)
throw_(amount_error, _("Cannot unround an uninitialized amount"));
else if (keep_precision())
return *this;
return;
amount_t t(*this);
t._dup();
t.set_keep_precision(true);
return t;
_dup();
set_keep_precision(true);
}
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
sets the "keep precision" state back to false.
@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
the display precision of the commodity would have printed. */
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
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
utilize "scaling commodities". For example, an amount of \c 1h

View file

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

View file

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

View file

@ -409,9 +409,26 @@ public:
value_t abs() const;
value_t rounded() const;
value_t truncated() const;
value_t unrounded() const;
value_t rounded() const {
value_t temp(*this);
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 temp(*this);