1

My current program makes a pizza order, if you enter

moho

which is a medium pizza,olives,ham,olives the price output should be:

Medium pizza with,olives,ham,olives,£7.00

however, I am getting:

Your order is: Medium pizza with,olives,ham,olives,£6.999999999999999

How can I make it so that it prints the correct output? I have gone through the code but can't seem to figure out why.

    public class pizza {
    public static void main(String[] args){
        pizzaServiceA();
    }
    public static void pizzaServiceA(){
        Scanner input = new Scanner(System.in);
        double total = 0;
        System.out.println("Enter order:");
        String order = input.next();
        String pizza = "Your order is: ";

        if (order.equals("quit")){
            System.out.println("Program exiting.");
            System.exit(0);
        }

        boolean size = false;
        for (int x = 0; x < order.length(); x++){
        if (order.charAt(0) == 'm' || order.charAt(0) == 'l'){
            if (order.charAt(x) == 'm'){
                total +=4.00;
                pizza += "Medium pizza with,";
                size = true;
            }
            else if (order.charAt(x) == 'l'){
                total +=5.00;
                pizza +="Large pizza with,";
            }
            else if (order.charAt(x) == 'h'){
                pizza +="ham,";
                if (size) total += 1.40; 
                else total +=2.10;
            }
            else if (order.charAt(x)== 'o'){
                pizza +="olives,";
                if(size) total +=0.80;
                else total +=1.20;
            }
            else if (order.charAt(x)=='p'){
                pizza+="pineapple,";
                if(size) total +=1;
                else total+=1.50;
            }
            else if (order.charAt(x)=='s'){
                pizza+="spinach,";
                if(size) total +=0.80;
                else total+=1.20;
            }
            else if (order.charAt(x)=='m'){
                pizza +="mozarella,";
                if(size) total =+ 1;
                else total+=1.50;
            }
        }
        else {
            System.out.println("Your first character must be m(medium) or l(large)");
        }
      }
        System.out.println(pizza +"£" + total + "0");
    }
}

note: I understand I am maybe not writing this code in an efficient way as I'm only a beginner in Java and I'm trying to learn.

  • 1
    @ΦXocę웃Пepeúpaツ thanks for the ever so helpful comment :) –  Apr 04 '17 at 13:25

4 Answers4

2

This epitomises the pitfalls in using a binary floating point type to represent exact money values.

For an easy life (especially given that you're a beginner), use an integral type (e.g. long) and work in pence.

For more details, see Is floating point math broken?

Community
  • 1
  • 1
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

If you wanna try dirty:

double total = 651.5176515121351;    
total = Math.round(total * 100);
total = total /100;

Lots of options:

String result = String.format("%.2f", total );
Alex
  • 419
  • 6
  • 24
  • this is good solution if you would like to force the result to always have $?.00. what if the price change to total $5.83 then rounding it would be $6.00. so I think this is not all time solution if your price changes to have decimals. – Michael Seltene Apr 04 '17 at 14:01
  • 1
    @MichaelSeltene nope 5.83*100 = 583 rounds 583=583 then 583/100=5.83 so it works. – Alex Apr 04 '17 at 14:04
0

There are many ways to round up etc. However, have a look at http://www.javaranch.com/journal/2003/07/MoneyInJava.html that uses BigDecimal from math library. Hope this helps.

eg.. src: http://www.javaranch.com/journal/2003/07/MoneyInJava.html - BigDecimal to the Rescue!

import java.text.*;
import java.math.*;

public class addCurrency{
   public static void main(String[] args) {
      BigDecimal val1 = new BigDecimal("12.52");
      BigDecimal val2 = new BigDecimal("17.21");
      BigDecimal tot = val1.add(val2);
      System.out.println(NumberFormat.getCurrencyInstance().format(tot));
   }
}
Michael Seltene
  • 543
  • 1
  • 5
  • 17
0
public class fungsi_umur {

    public static void umur(int tahunlahir, int tahunsekarang) {

        int umur = tahunsekarang - tahunlahir;
        System.out.print("Masukan umur :");
        System.out.print("Masukan nama :");

        if (umur <= 12){
            System.out.print("Umur Saya = "+umur);
            System.out.print("Saya Adalah Anak Anak");
            System.out.print("umur<= 12 ");umur(tahunlahir,tahunsekarang);


        }else if (umur > 12 && umur <= 18){
            System.out.println("Umur Saya = "+umur);
            System.out.println("Saya Adalah Remaja");umur(tahunlahir,tahunsekarang);
            System.out.print("umur > 12 && umur <= 18");umur(tahunlahir,tahunsekarang);

        }else if (umur > 18){
            System.out.println("Umur Saya = "+umur);
            System.out.println("Saya Adalah Dewasa");umur(tahunlahir,tahunsekarang);
            System.out.print("umur > 18");umur(tahunlahir,tahunsekarang);
        }
    }
    public static void main(String[] args) {

    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35