QuantLib: Money

In the previous article the Currency class was introduced, which defines all the relevant information associated with a currency. The Money class packs a Currency object together with a Decimal to define an amount of money in a specific currency. Money can be added and subtracted. If two instances of Money represent money in different currencies, conversion from one currency to another is performed automatically. This exchange is handled by the ExchangeRateManager, which will be treated in another article.

Money defines two self explanatory constructors

Money::Money(const Currency& currency, Decimal value);
Money::Money(Decimal value, const Currency& currency);

This is done for convenience, so that the developer does not have to remember the order of the arguments. Often programmers will not call these constructors directly. The multiplication between a Decimal and a Currency has been redefined to return the appropriate Money object. This is why, for clarity, one will often see code like the following.

Currency gbp=GBPCurrency();
Money m=78.38*gbp;

The two methods

const Currency& currency() const;
Decimal value() const;

return the associated Currency object and the value of the amount of money in that currency.

Money rounded() const;

This method returns a Money object that is a rounded version of the original money. The rounding is carried out according to the rounding convention defined by the currency.

Money can be written directly into a std::ostream. The overloaded << operator uses boost::format and the format string returned by Currency::format() to create human readable output. The code

Money m1 = 25.23*GBPCurrency();
Money m2 = 34.50*USDCurrency();
std::cout << "m1 = " << m1 << std::endl;
std::cout << "m2 = " << m2 << std::endl;

will produce the following output

m1 = £ 25.23
m2 = $ 34.50

Calculating with Money

Money can be multiplied with, or divided by a decimal number. As one would expect, this will affect the value and leave the currency unchanged.

Money a(100, GBPCurrency());
Money b = 1.2*a;
a = b/2.5;
std::cout << "a is " << a << std::endl;
std::cout << "b is " << b << std::endl;

This produces the output

a is £ 48.00
b is £ 120.00

Money can also be added and subtracted. If the currencies of the two operands are the same then this also amounts to a simple addition or subtraction of the values.

Money a(100, GBPCurrency());
Money b(25.52, GBPCurrency());
Money c = a - b;
Money d = a + b;
std::cout << "c is " << c << std::endl;
std::cout << "d is " << d << std::endl;

This produces the output

c is £ 74.48
d is £ 125.52

If the currencies are not identical then a conversion between the two currencies is attempted. This is done by the ExchangeRateManager which will be treated in a separate article.

In the same manner the comparison operators ==, !=, > and < allow you to compare two money objects. As above, if the currencies match the comparison just looks at the money values. If the currencies do not match, conversion is attempted.