thanks. could you give a little more details?
How can I fix it to ensure that the subtotal can get the right answer after the loop?
import java.math.BigDecimal;
public class test {
public static void main (String [] args) {
int a = 11;
BigDecimal b = new BigDecimal("17.39");
BigDecimal c = b.multiply(new BigDecimal(a));
System.out.println(c);
}
}
you could always display it as a string and chop off decimal places
public class Coins {
private long cents;
public Coins()
{
this(0.0);
}
public Coins(double value)
{
setValue(value);
}
public double getValue()
{
return this.cents / 100.0;
}
public void setValue(double value)
{
if (value >= 0)
this.cents = (long) (value * 100.0 + 0.5);
else
this.cents = (long) (value * 100.0 - 0.5);
}
public boolean areSame(Object anotherObject)
{
if (!(anotherObject instanceof Coins))
return false;
return getValue() == ((Coins)anotherObject).getValue();
}
protected long getCents()
{
return this.cents;
}
public boolean isBiggerValue(Coins any)
{
if (getCents() > any.getCents())
return true;
return false;
}
public boolean isLowerValue(Coins any)
{
if (getCents() < any.getCents())
return true;
return false;
}
public boolean sameValues(Coins any)
{
if (getCents() == any.getCents())
return true;
return false;
}
protected void setCents(long cents)
{
this.cents = cents;
}
}
I've made this class to work with these problems using double/float when working with cash and stuff.
It is kind of you to provide a lot of approaches to solve the problem. I just pick up the *100 then /100 method due to my simple homework. I shall learn to use the other sophisticated methods in the future. Thank you very much.🙂Especial for Wowzera providing his own class.