1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /* 4 Copyright (C) 2004 Decillion Pty(Ltd) 5 Copyright (C) 2004, 2005, 2006 StatPro Italia srl 6 7 This file is part of QuantLib, a free-software/open-source library 8 for financial quantitative analysts and developers - http://quantlib.org/ 9 10 QuantLib is free software: you can redistribute it and/or modify it 11 under the terms of the QuantLib license. You should have received a 12 copy of the license along with this program; if not, please email 13 <quantlib-dev@lists.sf.net>. The license is also available online at 14 <http://quantlib.org/license.shtml>. 15 16 This program is distributed in the hope that it will be useful, but WITHOUT 17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 18 FOR A PARTICULAR PURPOSE. See the license for more details. 19 */ 20 21 /*! \file money.hpp 22 \brief cash amount in a given currency 23 */ 24 25 #ifndef quantlib_money_hpp 26 #define quantlib_money_hpp 27 28 #include <ql/currency.hpp> 29 30 namespace QuantLib { 31 32 //! amount of cash 33 /*! \test money arithmetic is tested with and without currency 34 conversions. 35 */ 36 class Money { 37 public: 38 //! \name Constructors 39 //@{ 40 Money(); 41 Money(const Currency& currency, Decimal value); 42 Money(Decimal value, const Currency& currency); 43 //@} 44 //! \name Inspectors 45 //@{ 46 const Currency& currency() const; 47 Decimal value() const; 48 Money rounded() const; 49 //@} 50 /*! \name Money arithmetics 51 52 See below for non-member functions and for settings which 53 determine the behavior of the operators. 54 */ 55 //@{ 56 Money operator+() const; 57 Money operator-() const; 58 Money& operator+=(const Money&); 59 Money& operator-=(const Money&); 60 Money& operator*=(Decimal); 61 Money& operator/=(Decimal); 62 //@} 63 /*! \name Conversion settings 64 65 These parameters are used for combining money amounts 66 in different currencies 67 */ 68 //@{ 69 enum ConversionType { 70 NoConversion, /*!< do not perform conversions */ 71 BaseCurrencyConversion, /*!< convert both operands to 72 the base currency before 73 converting */ 74 AutomatedConversion /*!< return the result in the 75 currency of the first 76 operand */ 77 }; 78 static ConversionType conversionType; 79 static Currency baseCurrency; 80 //@} 81 private: 82 Decimal value_; 83 Currency currency_; 84 }; 85 86 87 // More arithmetics and comparisons 88 89 /*! \relates Money */ 90 Money operator+(const Money&, const Money&); 91 /*! \relates Money */ 92 Money operator-(const Money&, const Money&); 93 /*! \relates Money */ 94 Money operator*(const Money&, Decimal); 95 /*! \relates Money */ 96 Money operator*(Decimal, const Money&); 97 /*! \relates Money */ 98 Money operator/(const Money&, Decimal); 99 /*! \relates Money */ 100 Decimal operator/(const Money&, const Money&); 101 102 /*! \relates Money */ 103 bool operator==(const Money&, const Money&); 104 /*! \relates Money */ 105 bool operator!=(const Money&, const Money&); 106 /*! \relates Money */ 107 bool operator<(const Money&, const Money&); 108 /*! \relates Money */ 109 bool operator<=(const Money&, const Money&); 110 /*! \relates Money */ 111 bool operator>(const Money&, const Money&); 112 /*! \relates Money */ 113 bool operator>=(const Money&, const Money&); 114 115 /*! \relates Money */ 116 bool close(const Money&, const Money&, Size n = 42); 117 /*! \relates Money */ 118 bool close_enough(const Money&, const Money&, Size n = 42); 119 120 // syntactic sugar 121 122 /*! \relates Money */ 123 Money operator*(Decimal, const Currency&); 124 /*! \relates Money */ 125 Money operator*(const Currency&, Decimal); 126 127 // formatting 128 129 /*! \relates Money */ 130 std::ostream& operator<<(std::ostream&, const Money&); 131 132 133 // inline definitions 134 Money()135 inline Money::Money() 136 : value_(0.0) {} 137 Money(const Currency & currency,Decimal value)138 inline Money::Money(const Currency& currency, Decimal value) 139 : value_(value), currency_(currency) {} 140 Money(Decimal value,const Currency & currency)141 inline Money::Money(Decimal value, const Currency& currency) 142 : value_(value), currency_(currency) {} 143 currency() const144 inline const Currency& Money::currency() const { 145 return currency_; 146 } 147 value() const148 inline Decimal Money::value() const { 149 return value_; 150 } 151 rounded() const152 inline Money Money::rounded() const { 153 return Money(currency_.rounding()(value_), currency_); 154 } 155 operator +() const156 inline Money Money::operator+() const { 157 return *this; 158 } 159 operator -() const160 inline Money Money::operator-() const { 161 return Money(-value_, currency_); 162 } 163 operator *=(Decimal x)164 inline Money& Money::operator*=(Decimal x) { 165 value_ *= x; 166 return *this; 167 } 168 operator /=(Decimal x)169 inline Money& Money::operator/=(Decimal x) { 170 value_ /= x; 171 return *this; 172 } 173 174 operator +(const Money & m1,const Money & m2)175 inline Money operator+(const Money& m1, const Money& m2) { 176 Money tmp = m1; 177 tmp += m2; 178 return tmp; 179 } 180 operator -(const Money & m1,const Money & m2)181 inline Money operator-(const Money& m1, const Money& m2) { 182 Money tmp = m1; 183 tmp -= m2; 184 return tmp; 185 } 186 operator *(const Money & m,Decimal x)187 inline Money operator*(const Money& m, Decimal x) { 188 Money tmp = m; 189 tmp *= x; 190 return tmp; 191 } 192 operator *(Decimal x,const Money & m)193 inline Money operator*(Decimal x, const Money& m) { 194 return m*x; 195 } 196 operator /(const Money & m,Decimal x)197 inline Money operator/(const Money& m, Decimal x) { 198 Money tmp = m; 199 tmp /= x; 200 return tmp; 201 } 202 operator !=(const Money & m1,const Money & m2)203 inline bool operator!=(const Money& m1, const Money& m2) { 204 return !(m1 == m2); 205 } 206 operator >(const Money & m1,const Money & m2)207 inline bool operator>(const Money& m1, const Money& m2) { 208 return m2 < m1; 209 } 210 operator >=(const Money & m1,const Money & m2)211 inline bool operator>=(const Money& m1, const Money& m2) { 212 return m2 <= m1; 213 } 214 operator *(Decimal value,const Currency & c)215 inline Money operator*(Decimal value, const Currency& c) { 216 return Money(value,c); 217 } 218 operator *(const Currency & c,Decimal value)219 inline Money operator*(const Currency& c, Decimal value) { 220 return Money(value,c); 221 } 222 223 } 224 225 226 #endif 227