Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/mymoney/mymoneymoney.cpp
Context not available. | |||||
5 | copyright : (C) 2000-2002 by Michael Edwardes <mte@users.sourceforge.net> | 5 | copyright : (C) 2000-2002 by Michael Edwardes <mte@users.sourceforge.net> | ||
---|---|---|---|---|---|
6 | (C) 2011 by Carlos Eduardo da Silva <kaduardo@gmail.com> | 6 | (C) 2011 by Carlos Eduardo da Silva <kaduardo@gmail.com> | ||
7 | (C) 2001-2017 by Thomas Baumgart <tbaumgart@kde.org> | 7 | (C) 2001-2017 by Thomas Baumgart <tbaumgart@kde.org> | ||
8 | (C) 2017 by Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> | ||||
8 | ***************************************************************************/ | 9 | ***************************************************************************/ | ||
9 | 10 | | |||
10 | /*************************************************************************** | 11 | /*************************************************************************** | ||
Context not available. | |||||
24 | #include "mymoneymoney.h" | 25 | #include "mymoneymoney.h" | ||
25 | 26 | | |||
26 | #include <stdint.h> | 27 | #include <stdint.h> | ||
28 | #include <gmpxx.h> | ||||
27 | 29 | | |||
28 | // ---------------------------------------------------------------------------- | 30 | // ---------------------------------------------------------------------------- | ||
29 | // QT Includes | 31 | // QT Includes | ||
30 | 32 | | |||
33 | #include <QString> | ||||
34 | | ||||
31 | // ---------------------------------------------------------------------------- | 35 | // ---------------------------------------------------------------------------- | ||
32 | // Project Includes | 36 | // Project Includes | ||
33 | 37 | | |||
38 | #include "mymoneyexception.h" | ||||
39 | #include "mymoneyenums.h" | ||||
34 | 40 | | |||
35 | const MyMoneyMoney MyMoneyMoney::ONE = MyMoneyMoney(1, 1); | 41 | const MyMoneyMoney MyMoneyMoney::ONE = MyMoneyMoney(1, 1); | ||
36 | const MyMoneyMoney MyMoneyMoney::MINUS_ONE = MyMoneyMoney(-1, 1); | 42 | const MyMoneyMoney MyMoneyMoney::MINUS_ONE = MyMoneyMoney(-1, 1); | ||
43 | namespace eMyMoney | ||||
44 | { | ||||
45 | namespace Money { | ||||
46 | | ||||
47 | enum fileVersionE : int { | ||||
48 | FILE_4_BYTE_VALUE = 0, | ||||
49 | FILE_8_BYTE_VALUE | ||||
50 | }; | ||||
51 | | ||||
52 | QChar _thousandSeparator = QLatin1Char(','); | ||||
53 | QChar _decimalSeparator = QLatin1Char('.'); | ||||
54 | eMyMoney::Money::signPosition _negativeMonetarySignPosition = BeforeQuantityMoney; | ||||
55 | eMyMoney::Money::signPosition _positiveMonetarySignPosition = BeforeQuantityMoney; | ||||
56 | bool _negativePrefixCurrencySymbol = false; | ||||
57 | bool _positivePrefixCurrencySymbol = false; | ||||
58 | eMyMoney::Money::fileVersionE _fileVersion = fileVersionE::FILE_4_BYTE_VALUE; | ||||
59 | } | ||||
60 | } | ||||
37 | 61 | | |||
38 | QChar MyMoneyMoney::_thousandSeparator = ','; | 62 | //eMyMoney::Money::_thousandSeparator = QLatin1Char(','); | ||
39 | QChar MyMoneyMoney::_decimalSeparator = '.'; | 63 | //eMyMoney::Money::_decimalSeparator = QLatin1Char('.'); | ||
40 | MyMoneyMoney::signPosition MyMoneyMoney::_negativeMonetarySignPosition = BeforeQuantityMoney; | 64 | //eMyMoney::Money::signPosition eMyMoney::Money::_negativeMonetarySignPosition = BeforeQuantityMoney; | ||
41 | MyMoneyMoney::signPosition MyMoneyMoney::_positiveMonetarySignPosition = BeforeQuantityMoney; | 65 | //eMyMoney::Money::signPosition eMyMoney::Money::_positiveMonetarySignPosition = BeforeQuantityMoney; | ||
42 | bool MyMoneyMoney::_negativePrefixCurrencySymbol = false; | 66 | //bool eMyMoney::Money::_negativePrefixCurrencySymbol = false; | ||
43 | bool MyMoneyMoney::_positivePrefixCurrencySymbol = false; | 67 | //bool eMyMoney::Money::_positivePrefixCurrencySymbol = false; | ||
44 | 68 | | |||
45 | MyMoneyMoney::fileVersionE MyMoneyMoney::_fileVersion = MyMoneyMoney::FILE_4_BYTE_VALUE; | 69 | //MyMoneyMoney::fileVersionE eMyMoney::Money::_fileVersion = MyMoneyMoney::FILE_4_BYTE_VALUE; | ||
46 | 70 | | |||
47 | MyMoneyMoney MyMoneyMoney::maxValue = MyMoneyMoney(INT64_MAX, 100); | 71 | MyMoneyMoney MyMoneyMoney::maxValue = MyMoneyMoney(INT64_MAX, 100); | ||
48 | MyMoneyMoney MyMoneyMoney::minValue = MyMoneyMoney(INT64_MIN, 100); | 72 | MyMoneyMoney MyMoneyMoney::minValue = MyMoneyMoney(INT64_MIN, 100); | ||
Context not available. | |||||
50 | 74 | | |||
51 | void MyMoneyMoney::setNegativePrefixCurrencySymbol(const bool flag) | 75 | void MyMoneyMoney::setNegativePrefixCurrencySymbol(const bool flag) | ||
52 | { | 76 | { | ||
53 | _negativePrefixCurrencySymbol = flag; | 77 | eMyMoney::Money::_negativePrefixCurrencySymbol = flag; | ||
54 | } | 78 | } | ||
55 | 79 | | |||
56 | void MyMoneyMoney::setPositivePrefixCurrencySymbol(const bool flag) | 80 | void MyMoneyMoney::setPositivePrefixCurrencySymbol(const bool flag) | ||
57 | { | 81 | { | ||
58 | _positivePrefixCurrencySymbol = flag; | 82 | eMyMoney::Money::_positivePrefixCurrencySymbol = flag; | ||
59 | } | 83 | } | ||
60 | 84 | | |||
61 | void MyMoneyMoney::setNegativeMonetarySignPosition(const signPosition pos) | 85 | void MyMoneyMoney::setNegativeMonetarySignPosition(const eMyMoney::Money::signPosition pos) | ||
62 | { | 86 | { | ||
63 | _negativeMonetarySignPosition = pos; | 87 | eMyMoney::Money::_negativeMonetarySignPosition = pos; | ||
64 | } | 88 | } | ||
65 | 89 | | |||
66 | MyMoneyMoney::signPosition MyMoneyMoney::negativeMonetarySignPosition() | 90 | eMyMoney::Money::signPosition MyMoneyMoney::negativeMonetarySignPosition() | ||
67 | { | 91 | { | ||
68 | return _negativeMonetarySignPosition; | 92 | return eMyMoney::Money::_negativeMonetarySignPosition; | ||
69 | } | 93 | } | ||
70 | 94 | | |||
71 | void MyMoneyMoney::setPositiveMonetarySignPosition(const signPosition pos) | 95 | void MyMoneyMoney::setPositiveMonetarySignPosition(const eMyMoney::Money::signPosition pos) | ||
72 | { | 96 | { | ||
73 | _positiveMonetarySignPosition = pos; | 97 | eMyMoney::Money::_positiveMonetarySignPosition = pos; | ||
74 | } | 98 | } | ||
75 | 99 | | |||
76 | MyMoneyMoney::signPosition MyMoneyMoney::positiveMonetarySignPosition() | 100 | eMyMoney::Money::signPosition MyMoneyMoney::positiveMonetarySignPosition() | ||
77 | { | 101 | { | ||
78 | return _positiveMonetarySignPosition; | 102 | return eMyMoney::Money::_positiveMonetarySignPosition; | ||
79 | } | 103 | } | ||
80 | 104 | | |||
81 | void MyMoneyMoney::setThousandSeparator(const QChar &separator) | 105 | void MyMoneyMoney::setThousandSeparator(const QChar &separator) | ||
82 | { | 106 | { | ||
83 | if (separator != ' ') | 107 | if (separator != QLatin1Char(' ')) | ||
84 | _thousandSeparator = separator; | 108 | eMyMoney::Money::_thousandSeparator = separator; | ||
85 | else | 109 | else | ||
86 | _thousandSeparator = 0; | 110 | eMyMoney::Money::_thousandSeparator = 0; | ||
87 | } | 111 | } | ||
88 | 112 | | |||
89 | const QChar MyMoneyMoney::thousandSeparator() | 113 | const QChar MyMoneyMoney::thousandSeparator() | ||
90 | { | 114 | { | ||
91 | return _thousandSeparator; | 115 | return eMyMoney::Money::_thousandSeparator; | ||
92 | } | 116 | } | ||
93 | 117 | | |||
94 | void MyMoneyMoney::setDecimalSeparator(const QChar &separator) | 118 | void MyMoneyMoney::setDecimalSeparator(const QChar &separator) | ||
95 | { | 119 | { | ||
96 | if (separator != ' ') | 120 | if (separator != QLatin1Char(' ')) | ||
97 | _decimalSeparator = separator; | 121 | eMyMoney::Money::_decimalSeparator = separator; | ||
98 | else | 122 | else | ||
99 | _decimalSeparator = 0; | 123 | eMyMoney::Money::_decimalSeparator = 0; | ||
100 | } | 124 | } | ||
101 | 125 | | |||
102 | const QChar MyMoneyMoney::decimalSeparator() | 126 | const QChar MyMoneyMoney::decimalSeparator() | ||
103 | { | 127 | { | ||
104 | return _decimalSeparator; | 128 | return eMyMoney::Money::_decimalSeparator; | ||
105 | } | 129 | } | ||
106 | 130 | | |||
107 | void MyMoneyMoney::setFileVersion(fileVersionE version) | 131 | MyMoneyMoney::MyMoneyMoney(const QString& pszAmount) | ||
132 | : AlkValue(pszAmount, eMyMoney::Money::_decimalSeparator) | ||||
108 | { | 133 | { | ||
109 | _fileVersion = version; | | |||
110 | } | 134 | } | ||
111 | 135 | | |||
136 | //////////////////////////////////////////////////////////////////////////////// | ||||
137 | // Name: MyMoneyMoney | ||||
138 | // Purpose: Constructor - constructs object from an amount in a signed64 value | ||||
139 | // Returns: None | ||||
140 | // Throws: Nothing. | ||||
141 | // Arguments: Amount - signed 64 object containing amount | ||||
142 | // denom - denominator of the object | ||||
143 | // | ||||
144 | //////////////////////////////////////////////////////////////////////////////// | ||||
145 | MyMoneyMoney::MyMoneyMoney(signed64 Amount, const signed64 denom) | ||||
146 | { | ||||
147 | if (!denom) | ||||
148 | throw MYMONEYEXCEPTION("Denominator 0 not allowed!"); | ||||
112 | 149 | | |||
113 | MyMoneyMoney::MyMoneyMoney(const QString& pszAmount) | 150 | *this = AlkValue(QString::fromLatin1("%1/%2").arg(Amount).arg(denom), eMyMoney::Money::_decimalSeparator); | ||
114 | : AlkValue(pszAmount, _decimalSeparator) | 151 | } | ||
152 | | ||||
153 | //////////////////////////////////////////////////////////////////////////////// | ||||
154 | // Name: MyMoneyMoney | ||||
155 | // Purpose: Constructor - constructs object from an amount in a integer value | ||||
156 | // Returns: None | ||||
157 | // Throws: Nothing. | ||||
158 | // Arguments: iAmount - integer object containing amount | ||||
159 | // denom - denominator of the object | ||||
160 | // | ||||
161 | //////////////////////////////////////////////////////////////////////////////// | ||||
162 | MyMoneyMoney::MyMoneyMoney(const int iAmount, const signed64 denom) | ||||
163 | { | ||||
164 | if (!denom) | ||||
165 | throw MYMONEYEXCEPTION("Denominator 0 not allowed!"); | ||||
166 | *this = AlkValue(iAmount, denom); | ||||
167 | } | ||||
168 | | ||||
169 | //////////////////////////////////////////////////////////////////////////////// | ||||
170 | // Name: MyMoneyMoney | ||||
171 | // Purpose: Constructor - constructs object from an amount in a long integer value | ||||
172 | // Returns: None | ||||
173 | // Throws: Nothing. | ||||
174 | // Arguments: iAmount - integer object containing amount | ||||
175 | // denom - denominator of the object | ||||
176 | // | ||||
177 | //////////////////////////////////////////////////////////////////////////////// | ||||
178 | MyMoneyMoney::MyMoneyMoney(const long int iAmount, const signed64 denom) | ||||
115 | { | 179 | { | ||
180 | if (!denom) | ||||
181 | throw MYMONEYEXCEPTION("Denominator 0 not allowed!"); | ||||
182 | *this = AlkValue(QString::fromLatin1("%1/%2").arg(iAmount).arg(denom), eMyMoney::Money::_decimalSeparator); | ||||
116 | } | 183 | } | ||
117 | 184 | | |||
185 | | ||||
118 | MyMoneyMoney::~MyMoneyMoney() | 186 | MyMoneyMoney::~MyMoneyMoney() | ||
119 | { | 187 | { | ||
120 | } | 188 | } | ||
Context not available. | |||||
126 | 194 | | |||
127 | QString MyMoneyMoney::formatMoney(int denom, bool showThousandSeparator) const | 195 | QString MyMoneyMoney::formatMoney(int denom, bool showThousandSeparator) const | ||
128 | { | 196 | { | ||
129 | return formatMoney("", denomToPrec(denom), showThousandSeparator); | 197 | return formatMoney(QString(), denomToPrec(denom), showThousandSeparator); | ||
130 | } | 198 | } | ||
131 | 199 | | |||
132 | QString MyMoneyMoney::formatMoney(const QString& currency, const int prec, bool showThousandSeparator) const | 200 | QString MyMoneyMoney::formatMoney(const QString& currency, const int prec, bool showThousandSeparator) const | ||
Context not available. | |||||
193 | if (decimalSeparator() != 0) | 261 | if (decimalSeparator() != 0) | ||
194 | res += decimalSeparator(); | 262 | res += decimalSeparator(); | ||
195 | 263 | | |||
196 | QString rs = QString("%1").arg(right.get_str().c_str()); | 264 | auto rs = QString::fromLatin1("%1").arg(right.get_str().c_str()); | ||
197 | if (prec != -1) | 265 | if (prec != -1) | ||
198 | rs = rs.rightJustified(prec, '0', true); | 266 | rs = rs.rightJustified(prec, QLatin1Char('0'), true); | ||
199 | else { | 267 | else { | ||
200 | rs = rs.rightJustified(9, '0', true); | 268 | rs = rs.rightJustified(9, QLatin1Char('0'), true); | ||
201 | // no trailing zeroes or decimal separators | 269 | // no trailing zeroes or decimal separators | ||
202 | while (rs.endsWith('0')) | 270 | while (rs.endsWith(QLatin1Char('0'))) | ||
203 | rs.truncate(rs.length() - 1); | 271 | rs.truncate(rs.length() - 1); | ||
204 | while (rs.endsWith(QChar(decimalSeparator()))) | 272 | while (rs.endsWith(decimalSeparator())) | ||
205 | rs.truncate(rs.length() - 1); | 273 | rs.truncate(rs.length() - 1); | ||
206 | } | 274 | } | ||
207 | res += rs; | 275 | res += rs; | ||
208 | } | 276 | } | ||
209 | 277 | | |||
210 | signPosition signpos = bNegative ? _negativeMonetarySignPosition : _positiveMonetarySignPosition; | 278 | eMyMoney::Money::signPosition signpos = bNegative ? eMyMoney::Money::_negativeMonetarySignPosition : eMyMoney::Money::_positiveMonetarySignPosition; | ||
211 | QString sign = bNegative ? "-" : ""; | 279 | auto sign = bNegative ? QString::fromLatin1("-") : QString(); | ||
212 | 280 | | |||
213 | switch (signpos) { | 281 | switch (signpos) { | ||
214 | case ParensAround: | 282 | case eMyMoney::Money::ParensAround: | ||
215 | res.prepend('('); | 283 | res.prepend(QLatin1Char('(')); | ||
216 | res.append(')'); | 284 | res.append(QLatin1Char(')')); | ||
217 | break; | 285 | break; | ||
218 | case BeforeQuantityMoney: | 286 | case eMyMoney::Money::BeforeQuantityMoney: | ||
219 | res.prepend(sign); | 287 | res.prepend(sign); | ||
220 | break; | 288 | break; | ||
221 | case AfterQuantityMoney: | 289 | case eMyMoney::Money::AfterQuantityMoney: | ||
222 | res.append(sign); | 290 | res.append(sign); | ||
223 | break; | 291 | break; | ||
224 | case BeforeMoney: | 292 | case eMyMoney::Money::BeforeMoney: | ||
225 | tmpCurrency.prepend(sign); | 293 | tmpCurrency.prepend(sign); | ||
226 | break; | 294 | break; | ||
227 | case AfterMoney: | 295 | case eMyMoney::Money::AfterMoney: | ||
228 | tmpCurrency.append(sign); | 296 | tmpCurrency.append(sign); | ||
229 | break; | 297 | break; | ||
230 | } | 298 | } | ||
231 | if (!tmpCurrency.isEmpty()) { | 299 | if (!tmpCurrency.isEmpty()) { | ||
232 | if (bNegative ? _negativePrefixCurrencySymbol : _positivePrefixCurrencySymbol) { | 300 | if (bNegative ? eMyMoney::Money::_negativePrefixCurrencySymbol : eMyMoney::Money::_positivePrefixCurrencySymbol) { | ||
233 | res.prepend(' '); | 301 | res.prepend(QLatin1Char(' ')); | ||
234 | res.prepend(tmpCurrency); | 302 | res.prepend(tmpCurrency); | ||
235 | } else { | 303 | } else { | ||
236 | res.append(' '); | 304 | res.append(QLatin1Char(' ')); | ||
237 | res.append(tmpCurrency); | 305 | res.append(tmpCurrency); | ||
238 | } | 306 | } | ||
239 | } | 307 | } | ||
Context not available. | |||||
314 | return (*this == autoCalc); | 382 | return (*this == autoCalc); | ||
315 | } | 383 | } | ||
316 | 384 | | |||
317 | MyMoneyMoney MyMoneyMoney::convert(const signed64 _denom, const roundingMethod how) const | 385 | MyMoneyMoney MyMoneyMoney::convert(const signed64 _denom, const AlkValue::RoundingMethod how) const | ||
318 | { | 386 | { | ||
319 | return static_cast<const MyMoneyMoney>(convertDenominator(_denom, static_cast<RoundingMethod>(how))); | 387 | return static_cast<const MyMoneyMoney>(convertDenominator(_denom, how)); | ||
320 | } | 388 | } | ||
321 | 389 | | |||
322 | MyMoneyMoney MyMoneyMoney::reduce() const | 390 | MyMoneyMoney MyMoneyMoney::reduce() const | ||
Context not available. |