86 lines
1.9 KiB
Java
86 lines
1.9 KiB
Java
class Money
|
|
{
|
|
private double mValue;
|
|
public Money()
|
|
{
|
|
setValue(0.00);
|
|
}
|
|
public Money(double value)
|
|
{
|
|
setValue(value);
|
|
}
|
|
public Money(Money money)
|
|
{
|
|
setValue(money.getValue());
|
|
}
|
|
public double getValue()
|
|
{
|
|
return mValue;
|
|
}
|
|
public void setValue(double value)
|
|
{
|
|
mValue=value;
|
|
}
|
|
public String toString()
|
|
{
|
|
String strDouble;
|
|
String strFixed;
|
|
String strFinal;
|
|
String strFraction;
|
|
int strLength;
|
|
int commaCount;
|
|
int initPos;
|
|
int lastPos;
|
|
int ptIndex;
|
|
|
|
lastPos=0;
|
|
strFraction=new String();
|
|
strFinal=new String();
|
|
strDouble=stringRep(getValue()); // strDouble=String.valueOf(getValue());
|
|
strLength=strDouble.length();
|
|
for(ptIndex=0;ptIndex<strLength&&strDouble.charAt(ptIndex)!='.';ptIndex++);
|
|
if(ptIndex>=strLength)strFraction=".00";
|
|
else strFraction=strDouble.substring(ptIndex,strLength);
|
|
if(2==strFraction.length())strFraction+="0";
|
|
strFixed=strDouble.substring(0,ptIndex);
|
|
strLength=strFixed.length();
|
|
initPos=strLength%3;
|
|
commaCount=strLength/3;
|
|
if(0==initPos)
|
|
{
|
|
initPos+=3;
|
|
commaCount--;
|
|
}
|
|
strFinal+="$";
|
|
for(int count=0;count<commaCount;count++)
|
|
{
|
|
strFinal+=strFixed.substring(lastPos,initPos);
|
|
strFinal+=",";
|
|
lastPos=initPos;
|
|
initPos+=3;
|
|
}
|
|
strFinal+=strFixed.substring(lastPos,initPos);
|
|
strFinal+=strFraction;
|
|
return strFinal;
|
|
}
|
|
private String stringRep(double value)
|
|
{
|
|
int ptIndex;
|
|
int strLength;
|
|
int strFractionLength;
|
|
String strRep;
|
|
String strFraction;
|
|
String strWhole;
|
|
|
|
strRep=String.valueOf(value+.005);
|
|
strLength=strRep.length();
|
|
for(ptIndex=0;ptIndex<strLength&&strRep.charAt(ptIndex)!='.';ptIndex++);
|
|
if(ptIndex>=strLength)return String.valueOf(value);
|
|
strFraction=strRep.substring(ptIndex+1,strLength);
|
|
strFractionLength=strFraction.length();
|
|
if(strFractionLength>2)strFraction=strRep.substring(ptIndex+1,strLength-(strFractionLength-2));
|
|
strWhole=strRep.substring(0,ptIndex);
|
|
return strWhole+"."+strFraction;
|
|
}
|
|
};
|