-2

How do I display a price up to two decimals (cents) in Java? I have tried different methods but can't seem to get it right.

import java.io.; import java.util.;

public class Menu {

    public static void main(String[] args){
            String apptName = "";
            String entreeName = "";
            String dessertName = "";

            double apptPrice = 0.00;
            double entreePrice = 0.00;
            double dessertPrice = 0.00;
                    
            
            try {
                    Scanner sc = new Scanner(new File("menu.txt"));
                    while (sc.hasNextLine()) {
                            String line = sc.nextLine();
                            String[] item = line.split(", ");
                            int type = Integer.parseInt(item[2]);
                            String name = item[0];
                            double price = Double.parseDouble(item[1].substring(1));
                            String s = String.format("$%.2f", 2.50);

                            if (type == 1) {
                                    if (price > apptPrice) {
                                            apptName = name;
                                            apptPrice = price;
                                    }
                            } else if (type == 2) {
                                    if (price > entreePrice) {
                                            entreeName = name;
                                            entreePrice = price;
                                    }
                            } else {
                                    if (price > dessertPrice) {
                                            dessertName = name;
                                            dessertPrice = price;
                                    }
                            }
                    }

                    double total = apptPrice + entreePrice + dessertPrice;

                    System.out.println("Please enter the filename you wish to process: Most Expensive Meal");
                    System.out.println("-------------------");
                    System.out.println("Appetizer  : "+apptName);
                    System.out.println("Entree     : "+entreeName);
                    System.out.println("Dessert    : "+dessertName);
                    System.out.printf("Total Price: $"+total);
                   
                    
            } catch (FileNotFoundException fe) {
                    System.out.println("Sorry, input file: was not found.");
            }
    }

}

https://i.stack.imgur.com/L3Ai8.png

1 Answers1

0

The easiest way to deal with money in Java is to use the BigDecimal class. https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html

It will make your code a lot cleaner.

You will declare your price variables like this:

BigDecimal dessertPrice = new BigDecimal(2.00);

You will also get the correct accuracy you need using this class, whereas with float or double you will have to worry about that. This class was made for monetary calculations.

Femke
  • 85
  • 8