87 lines
1.5 KiB
C++
87 lines
1.5 KiB
C++
#include <analytic/money.hpp>
|
|
#include <common/stdio.hpp>
|
|
|
|
Money::Money(void)
|
|
: mValue(0.00), mFormat("% 12.2lf")
|
|
{
|
|
}
|
|
|
|
Money::Money(double value)
|
|
: mValue(value), mFormat("% 12.2lf")
|
|
{
|
|
}
|
|
|
|
Money::Money(const Money &someMoney)
|
|
{
|
|
*this=someMoney;
|
|
}
|
|
|
|
Money::~Money()
|
|
{
|
|
}
|
|
|
|
Money &Money::operator=(const Money &someMoney)
|
|
{
|
|
format(someMoney.format());
|
|
value(someMoney.value());
|
|
return *this;
|
|
}
|
|
|
|
void Money::format(const String &strFormat)
|
|
{
|
|
mFormat=strFormat;
|
|
}
|
|
|
|
const String &Money::format(void)const
|
|
{
|
|
return mFormat;
|
|
}
|
|
|
|
double Money::value(void)const
|
|
{
|
|
return mValue;
|
|
}
|
|
|
|
void Money::value(double value)
|
|
{
|
|
mValue=value;
|
|
}
|
|
|
|
Money::operator String(void)
|
|
{
|
|
String strString;
|
|
String moneyString;
|
|
|
|
::sprintf(strString,mFormat,mValue);
|
|
char *strPtr=strString;
|
|
char *strRoot=strPtr;
|
|
while(*strPtr!='.')strPtr++;
|
|
while(TRUE)
|
|
{
|
|
for(int count=0;count<3&&strPtr>strRoot&&*strPtr!=' ';count++)strPtr--;
|
|
if(strPtr<strRoot)break;
|
|
moneyString.insert(strPtr);
|
|
if(*strPtr==' '||strPtr==strRoot)break;
|
|
if(*(strPtr-1)!=' '&&*(strPtr-1)!='-')moneyString.insert(",");
|
|
*strPtr=0;
|
|
}
|
|
apply(moneyString);
|
|
return moneyString;
|
|
}
|
|
|
|
Money::Money(String strMoney)
|
|
: mFormat("% 12.2lf")
|
|
{
|
|
BOOL negate(FALSE);
|
|
strMoney.removeTokens("$,");
|
|
if(-1!=strMoney.strchr('(')){strMoney.removeTokens("(");strMoney.removeTokens(")");negate=TRUE;}
|
|
mValue=::atof(strMoney);
|
|
if(negate)mValue=-mValue;
|
|
}
|
|
|
|
// virtuals
|
|
void Money::apply(String &strMoney)
|
|
{
|
|
strMoney.insert("$");
|
|
}
|