23 #include "Bigdecimal.h"
25 Bigdecimal::Bigdecimal() {
27 this->parts.push_back(Bigint::ZERO_INT);
28 this->decimalDigits = 0;
29 this->decimalStartsAt = 0;
32 Bigdecimal::~Bigdecimal() {
35 Bigdecimal::Bigdecimal(
string value)
40 void Bigdecimal::create(
string value)
44 string temp = StringUtil::trimCopy(value);
45 int minusSign = temp.find_last_of(
"-");
51 temp = temp.substr(1);
53 if(temp.find_first_not_of(
"0")==string::npos)
55 this->decimalStartsAt = 0;
56 this->decimalDigits = 0;
58 this->parts.push_back(Bigint::ZERO_INT);
61 temp = temp.substr(temp.find_first_not_of(
"0"));
62 if(temp.find(
".")!=string::npos)
64 if(temp.find(
".")!=temp.find_last_of(
"."))
66 throw "Invalid decimal Number";
70 decimalDigits = temp.length() - temp.find(
".") - 1;
71 decimalStartsAt = (int)temp.find(
".");
72 StringUtil::replaceFirst(temp,
".",
"");
73 temp = temp.substr(0, temp.find_last_not_of(
"0")+1);
78 this->decimalStartsAt = 0;
79 this->decimalDigits = 0;
81 while((
int)temp.length()>Bigint::NUM_LENGTH)
84 int x = CastUtil::lexical_cast<
int>(temp.substr(temp.length()-Bigint::NUM_LENGTH));
85 temp = temp.substr(0, temp.length()-Bigint::NUM_LENGTH);
88 throw "Invalid Bigdecimal value";
94 int x = CastUtil::lexical_cast<
int>(temp);
97 throw "Invalid Bigdecimal value";
100 if(parts.at(parts.size()-1)==0)
101 parts.erase(parts.begin()+(parts.size()-1));
105 void Bigdecimal::checkAndSetIfZero()
108 for (
int i=0;i<(int)parts.size();i++) {
115 if(!flag || parts.size()==0)
118 this->parts.push_back(Bigint::ZERO_INT);
125 string fval = toString();
126 string sval = number.toString();
127 int maxdecdigits = decimalDigits>number.decimalDigits?decimalDigits:number.decimalDigits;
128 for (
int j = 0; j < maxdecdigits-decimalDigits; j++) {
129 fval.append(Bigint::ZERO);
131 for (
int j = 0; j < maxdecdigits-number.decimalDigits; j++) {
132 sval.append(Bigint::ZERO);
134 StringUtil::replaceFirst(fval,
".",
"");
135 StringUtil::replaceFirst(sval,
".",
"");
139 this->parts = fnum.parts;
140 this->isPositive = fnum.isPositive;
141 decimalDigits = (decimalDigits>number.decimalDigits?decimalDigits:number.decimalDigits);
154 temp.subtract(number);
161 temp.multiply(number);
188 this->subtract(temp);
194 this->subtract(number);
200 if(lhs.compare(rhs)==0)
207 if(lhs.compare(rhs)!=0)
214 if(lhs.compare(rhs)==-1)
221 if(lhs.compare(rhs)<=0)
228 if(lhs.compare(rhs)==1)
235 if(lhs.compare(rhs)>=0)
242 string fval = toString();
243 string sval = number.toString();
244 int maxdecdigits = decimalDigits>number.decimalDigits?decimalDigits:number.decimalDigits;
245 for (
int j = 0; j < maxdecdigits-decimalDigits; j++) {
246 fval.append(Bigint::ZERO);
248 for (
int j = 0; j < maxdecdigits-number.decimalDigits; j++) {
249 sval.append(Bigint::ZERO);
251 StringUtil::replaceFirst(fval,
".",
"");
252 StringUtil::replaceFirst(sval,
".",
"");
256 this->parts = fnum.parts;
257 this->isPositive = fnum.isPositive;
258 decimalDigits = (decimalDigits>number.decimalDigits?decimalDigits:number.decimalDigits);
263 string fval = toString();
264 string sval = number.toString();
265 int maxdecdigits = decimalDigits>number.decimalDigits?decimalDigits:number.decimalDigits;
266 for (
int j = 0; j < maxdecdigits-decimalDigits; j++) {
267 fval.append(Bigint::ZERO);
269 for (
int j = 0; j < maxdecdigits-number.decimalDigits; j++) {
270 sval.append(Bigint::ZERO);
272 StringUtil::replaceFirst(fval,
".",
"");
273 StringUtil::replaceFirst(sval,
".",
"");
277 this->parts = fnum.parts;
278 this->isPositive = fnum.isPositive;
279 decimalDigits += number.decimalDigits;
282 void Bigdecimal::divide(
Bigdecimal number,
int precision)
284 string fval = toString();
285 string sval = number.toString();
286 int maxdecdigits = decimalDigits>number.decimalDigits?decimalDigits:number.decimalDigits;
287 for (
int j = 0; j < maxdecdigits-decimalDigits; j++) {
288 fval.append(Bigint::ZERO);
290 for (
int j = 0; j < maxdecdigits-number.decimalDigits; j++) {
291 sval.append(Bigint::ZERO);
293 StringUtil::replaceFirst(fval,
".",
"");
294 StringUtil::replaceFirst(sval,
".",
"");
297 fnum.internalDivide(sval,
true, precision-1);
298 this->parts = fnum.parts;
299 this->isPositive = fnum.isPositive;
300 this->decimalStartsAt = fnum.decimalStartsAt;
301 decimalDigits -= number.decimalDigits;
307 return number1.compare(number2);
312 if(isPositive==number.isPositive)
314 string fnvalue = toString();
315 string snvalue = number.toString();
317 if(decimalStartsAt>0)
319 Bigint temp(fnvalue.substr(0, decimalStartsAt));
327 if(number.decimalStartsAt>0)
329 Bigint temp(snvalue.substr(0, number.decimalStartsAt));
337 int compVal = fnum.compare(snum);
340 string fmantstr = fnvalue.substr(decimalStartsAt+1, decimalDigits);
341 string smantstr = snvalue.substr(number.decimalStartsAt+1, number.decimalDigits);
342 int maxdecdigits = decimalDigits>number.decimalDigits?decimalDigits:number.decimalDigits;
343 for (
int j = 0; j < maxdecdigits-decimalDigits; j++) {
344 fmantstr.append(Bigint::ZERO);
346 for (
int j = 0; j < maxdecdigits-number.decimalDigits; j++) {
347 smantstr.append(Bigint::ZERO);
349 Bigint fmantissa(fmantstr);
350 Bigint smantissa(smantstr);
351 return fmantissa.compare(smantissa);
355 else if(isPositive && !number.isPositive)
365 string Bigdecimal::toString()
370 vector<int> nparts = parts;
371 std::reverse(nparts.begin(),nparts.end());
374 build.append(Bigint::MINUS);
376 for (
int i=0;i<(int)nparts.size();i++) {
379 string numstr = CastUtil::lexical_cast<
string>(nparts.at(i));
380 for (
int j = 0; j < Bigint::NUM_LENGTH-(int)numstr.length(); j++) {
381 build.append(Bigint::ZERO);
383 build.append(numstr);
387 build.append(CastUtil::lexical_cast<string>(nparts.at(i)));
392 build = build.substr(0, build.find_last_not_of(
"0")+1);
393 build = build.substr(0, build.length()-decimalDigits) +
"." + build.substr(build.length()-decimalDigits);;
395 else if(decimalStartsAt>0)
397 build = build.substr(0, decimalStartsAt) +
"." + build.substr(decimalStartsAt);