0

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?

Community
  • 1
  • 1
Jeroen Vannevel
  • 41,258
  • 21
  • 92
  • 157

2 Answers2

4

This is the reality of floating-point numbers (double and float). They use a mathematical formula (I don't go into specifics) to store their data, so some values are not EXACTLY what you'd expect (this is why banks and other monetary systems use BigDecimal instead of double or other such things).

Since, in your case, you're dealing with reasonably small units of measure, I would perhaps just round it to some decimal places.

Here's a small bit of code to output, rounded to 2 decimal places:

String dblOutput = new DecimalFormat("#.##").format(icecream.getCost());
System.out.println("Cost: " + dblOutput + "\t" + "Description: " + icecream.getDescription());
Eric
  • 63,873
  • 22
  • 120
  • 135
1

This is just a result of floating point rounding. Keep in mind that in base 2 a nice terminating decimal, like 0.1 may be an infinite repeating decimal. When that decimal is rounded, we get these odd round off errors.

The solution is to never print floating point values directly. Use DecimalFormat in Java.text or get currency instance to format (and round) your output

Thorn
  • 3,935
  • 4
  • 20
  • 42