bankey
bankey

Reputation: 189

Java - double*int arithmatic displays with two decimals OR several decimal places

New to Java. I am writing a simple program for a project. It is a program that allows a user to buy pizza. It has a running total and an order summary at the end. I am using these lines to keep the total & subtotal:

switch(mainMenuAnswer) //Switch statement to get correct pricing multiplied by # of desired pizza ordered  
{            
    case 1:
        total = total + (pizzaPrices1[otherAnswer - 1] * count);
        miniTotal = pizzaPrices1[otherAnswer - 1] * count;
        break;

    case 2:
        total = total + (pizzaPrices2[otherAnswer - 1] * count);
        miniTotal = pizzaPrices2[otherAnswer - 1] * count;
        break;

    case 3:
        total = total + (pizzaPrices3[otherAnswer - 1] * count);
        miniTotal = pizzaPrices3[otherAnswer - 1] * count;
        break;

    case 4:
        total = total + (pizzaPrices4[otherAnswer - 1] * count);
        miniTotal = pizzaPrices4[otherAnswer - 1] * count;
        break;

    case 5:
        total = total + (pizzaPrices5[otherAnswer - 1] * count);
        miniTotal = pizzaPrices5[otherAnswer - 1] * count;
        break;

}

Total and miniTotal are doubles that starts out at 0.0, while the prices are in a ##.## format, followed by count, which is an integer value. They are being multiplied.

Sometimes I get a good answer, such as $42.35 (random number), but sometimes I get:

 How many large pizzas?: 4

---------------------------------------------------------

Your current order total is $151.91

---------------------------------------------------------

Specialty Pizza Menu

1) Meat Lovers
2) BBQ Chicken
3) Hawaiian
4) Chicken-Bacon Ranch
5) Vegetarian
6) Exit Menu

Your choice?: 6

Order Summary:

Type                   Size          Quantity    Price
---------------------------------------------------------
Meat Lovers            Large         5           $99.94999999999999 
BBQ Chicken            Large         4           $51.96 

Order total: $151.91
---------------------------------------------------------

Note that everything is being formatted (just in case) to two decimal places, except the double values directly under "Price". I would easily format it as well, but I don't know how. The line of code to format is:

System.out.printf("%-22s %-13s %-11d %-2s \n" , typeArray[i], sizeArray[i], quantityArray[i], priceArray[i]); //Formatting

I tried using %-11.2d but it gives me an error. Anybody know what is going on? I assume it is something with the variable double and memory bits, but what is a good solution?

NOTE: priceArray is a STRING array, in $##.## format.

Upvotes: 0

Views: 79

Answers (2)

pompanoSlayer
pompanoSlayer

Reputation: 163

The use of 11.2d is used for double formating, while your array is a String. You might want to use some string method to format it the way you want it to be.

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Find "." and then add two more index to it and cut off the rest.

Upvotes: 1

dz210
dz210

Reputation: 768

Since price array is a string array, you cannot give it number formatting. Keep it as a double array and use the double to 2 decimal string conversion at the end.

Upvotes: 1

Related Questions