Possible Duplicate:
Moving decimal places over in a double
I am creating a basic implementation of the Decorator pattern, using icecream and supplements as subject. Everything works fine, except for some weird output behaviour that popped up.
At seemingly random places the cost will be output with a rounding at the 16th element behind the dot, which is weird because the numbers I use are 1.00 (Vanilla
), 1.10 (Chocolate
), 0.30 (Sprinkles
), 0.40 (WhippedCream
) and 0.35 (Nuts
).
In order to recreate this you'll need the following classes and interfaces:
public interface Icecream {
public double getCost();
public String getDescription();
}
public class Vanilla implements Icecream {
@Override
public double getCost() {
return 1.00;
}
@Override
public String getDescription() {
return "Vanilla ice";
}
}
public class Chocolate implements Icecream {
@Override
public double getCost() {
return 1.10;
}
@Override
public String getDescription() {
return "Chocolate ice";
}
}
public abstract class Decorator implements Icecream{
protected Icecream decoratedIcecream;
public Decorator(Icecream decoratedIcecream) {
this.decoratedIcecream = decoratedIcecream;
}
public abstract double getCost();
public abstract String getDescription();
}
public class Sprinkles extends Decorator {
public Sprinkles(Icecream decoratedIcecream) {
super(decoratedIcecream);
}
@Override
public double getCost() {
return decoratedIcecream.getCost() + 0.30;
}
@Override
public String getDescription() {
return decoratedIcecream.getDescription() + ", Sprinkles";
}
}
public class WhippedCream extends Decorator {
public WhippedCream(Icecream decoratedIcecream) {
super(decoratedIcecream);
}
@Override
public double getCost() {
return decoratedIcecream.getCost() + 0.40;
}
@Override
public String getDescription() {
return decoratedIcecream.getDescription() + ", Whipped cream";
}
}
public class Nuts extends Decorator {
public Nuts(Icecream decoratedIcecream) {
super(decoratedIcecream);
}
@Override
public double getCost() {
return decoratedIcecream.getCost() + 0.35;
}
@Override
public String getDescription() {
return decoratedIcecream.getDescription() + ", Nuts";
}
}
public class StartUp {
public static void main(String[] args) {
Icecream icecream = new Vanilla();
showDetails(icecream);
icecream = new Sprinkles(icecream);
showDetails(icecream);
icecream = new WhippedCream(icecream);
showDetails(icecream);
Icecream icecream2 = new Nuts(new Chocolate());
showDetails(icecream2);
icecream2 = new Sprinkles(icecream2);
showDetails(icecream2);
Icecream icecream3 = new Vanilla();
showDetails(icecream3);
icecream3 = new Nuts(icecream3);
showDetails(icecream3);
icecream3 = new Sprinkles(icecream3);
showDetails(icecream3);
}
private static void showDetails(Icecream icecream){
System.out.println("Cost: " + icecream.getCost() + "\t" + "Description: " + icecream.getDescription());
}
}
Which results in the following output:
Cost: 1.0 Description: Vanilla ice
Cost: 1.3 Description: Vanilla ice, Sprinkles
Cost: 1.7000000000000002 Description: Vanilla ice, Sprinkles, Whipped cream
Cost: 1.4500000000000002 Description: Chocolate ice, Nuts
Cost: 1.7500000000000002 Description: Chocolate ice, Nuts, Sprinkles
Cost: 1.0 Description: Vanilla ice
Cost: 1.35 Description: Vanilla ice, Nuts
Cost: 1.6500000000000001 Description: Vanilla ice, Nuts, Sprinkles
I was reluctant to add this much code, but considering the error seems to pop up randomly (once when I added whipped cream, when I created chocolate + nuts, and twice when I added sprinkles) I can't really pinpoint what parts of my code are relevant and what ones aren't.
Why does it sometimes round a number that is declared as double everywhere and always has 2 digits behind the dot?