# How can I make the final total price look like currency?

• (2 Pages)
• 1
• 2

## 17 Replies - 1624 Views - Last Post: 08 May 2013 - 04:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=320373&amp;s=9933f653fb344fad52abec221175de5d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dondre2010

Reputation: 0
• Posts: 18
• Joined: 02-May 13

# How can I make the final total price look like currency?

Posted 05 May 2013 - 07:09 PM

How can I make the final total price look like currency? It just seems to stay as two digits, or repeated 9's

```import java.util.ArrayList;
import javax.swing.*;

/**
* This program works as an automated worker from
* Subway to take your order and then displays total cost of your order
* @author Zeus
* @version  1.0
*/
public class Subway {

public static void main(String[] args) {

double price = 0;
double total = 0;
double totalpriceofvegi = 0;
double totalpriceofsandwich = 0;
double totalpriceofcheese = 0;

/* Creates a sandwich object to later use and add the available Sandwiches
* and the prices of them to an arrayList
*/
Sandwich sndwich = new Sandwich();

/*Here is how a sandwiches are added to
* the arrayList one by one
* start
*/
sndwich.setSandwich("Oven Roasted Chicken", "3");
sndwich.setSandwich("Meatball Marinara", "4");
sndwich.setSandwich("Smokehouse BBQ Chiken", "5");
sndwich.setSandwich("Blackforest Ham", "6");
sndwich.setSandwich("BLT", "4");
sndwich.setSandwich("Cold Cut Combo", "4");
sndwich.setSandwich("Veggie Delite ", "3");
sndwich.setSandwich("Egg & Chicken", "3");

/*
*/

/*
* Type of veggies that a customer can
* include onion,pepper,tomato
* start
*/
Vegetables onion = new Onion();
/*
* setting the onion object name and the
* the price
*/
onion.Setname("Onion");
onion.SetPrice(0.5);

Vegetables pepper = new Pepper();
pepper.Setname("Pepper");
pepper.SetPrice(0.6);

Vegetables tomato = new Tomato();
tomato.Setname("Tomato");
tomato.SetPrice(0.7);

/*
* end of creating veggies
*/

/*
* This Cheese object will hold the available cheese types
* start
*/
Cheese cheeses = new Cheese();

/*
* adding Cheese object name value
*/
cheeses.SetCheesename("Swiss", "0.80");
cheeses.SetCheesename("Provolone", "0.75");
cheeses.SetCheesename("Cheddar", "0.70");
/*
* end of setting available cheese objects
*/

/*
* UI frame of the code
*/
JFrame frame = new JFrame();

/*
* getting user input of his name
*/
String name = (String) JOptionPane.showInputDialog(frame, "Enter your name", "SUBWAY", JOptionPane.QUESTION_MESSAGE, null, null, null);
if(name.equalsIgnoreCase("X"))
System.exit(0);

/*
* creating set of images as the menu images so that
* customer can look at them and choose from the menu
* start
*/
ImageIcon icon1 = new ImageIcon("photo1.JPG");
ImageIcon icon3 = new ImageIcon("photo3.JPG");
ImageIcon icon5 = new ImageIcon("photo5.JPG");
ImageIcon icon6 = new ImageIcon("photo6.JPG");
ImageIcon icon7 = new ImageIcon("photo7.JPG");
ImageIcon icon8 = new ImageIcon("photo8.JPG");
/*
* end of creating images
*/

/*
* this loop works until customer says he does not have
* any orders
*/

while (true) {

/*
* getting the sandwich size and store it in the
* size String object
*/
String size = (String) JOptionPane.showInputDialog(frame, "What size of sandwich would you like? 6 inch or 12 inch? (Type in 6 or 12)", "SUBWAY", JOptionPane.QUESTION_MESSAGE, icon1, null, null);
if(size.equalsIgnoreCase("X"))
System.exit(0);

/*
* getting the bread type and store it in the
*/
System.exit(0);

/* getting the sandwich customer need and store it
* in the sandwich string object
*/
String sandwich = (String) JOptionPane.showInputDialog(frame, "What kind of sandwich would you like? (Type in the name of sandwich of your choice) \nOven Roasted Chicken = \$3.00\n Meatball Marinara = \$4.00\nSmokehouse BBQ Chiken = \$5.00\nBlackforest Ham = \$6.00\nBLT = \$4.00\nCold Cut Combo = \$4.00\nVeggie Delite = \$3.00\nEgg & Chicken = \$3.00", "SUBWAY", JOptionPane.QUESTION_MESSAGE, icon3, null, null);
if(sandwich.equalsIgnoreCase("X"))
System.exit(0);

/*
* getting the cheese type customer need and
* store it in the cheese string object
*/
String cheese = (String) JOptionPane.showInputDialog(frame, "What kind of cheese would you like? (Type in the name of cheese of your choice)   \nSwiss = \$0.80\nProvolone = \$0.75\nCheddar = \$0.70", "SUBWAY", JOptionPane.QUESTION_MESSAGE, icon7, null, null);
if(cheese.equalsIgnoreCase("X"))
System.exit(0);

/*
* getting the input on user need it toasted or not
*/
String toastedORnot = (String)JOptionPane.showInputDialog(frame, "Do you want it toasted? (Type in Y for yes or N for no)", "SUBWAY", JOptionPane.QUESTION_MESSAGE, icon5, null, null);
if(toastedORnot.equalsIgnoreCase("X"))
System.exit(0);

/*
* getting the veggies user need and store them in veggies object
*/
String veggies = (String) JOptionPane.showInputDialog(frame, "What veggies would you like on it? (Type in the name of the veggie(s) you'd like on your sub) \nTomato = \$0.70\nPepper = \$0.60\nOnion = \$0.50?", "SUBWAY", JOptionPane.QUESTION_MESSAGE, icon6, null, null);
if(veggies.equalsIgnoreCase("X"))
System.exit(0);

/*
* marking whether customer need salt or not
*/
String saltinit = (String)JOptionPane.showInputDialog(frame, "Would you like salt on it? (Type in Y for yes or N for no)", "SUBWAY", JOptionPane.QUESTION_MESSAGE, icon5, null, null);
if(saltinit.equalsIgnoreCase("X"))
System.exit(0);

/*
* Asking whether customer has new orders!! and store the response
* in the finish object string
*/
String finish = (String) JOptionPane.showInputDialog(frame, "Would you like to buy another sandwich? (Type in Y for yes or N for no)", "SUBWAY", JOptionPane.QUESTION_MESSAGE, icon5, null, null);
if(finish.equalsIgnoreCase("X"))
System.exit(0);

/*
*/

/*
* price variable increase by bread price and also
* total price will be set to bread price only if @param bredprice variable
* is not null
*/

} else {
}

/*
* same as bread price retrieving
* here sandwich price is retrieved
*/
String sandwichprice = sndwich.sandwichcheckgetprice(sandwich);

/*
*check whether sandwich price is null or not and then
* check what type is that sandwich whether it is 12 size or 6
* and after that according to the sandwich type the value of totalpriceofsandwich
* is set
*/
if (sandwichprice != null) {
if (size.equals("12")) {
totalpriceofsandwich = 2 * (Double.parseDouble(sandwichprice));
} else if (size.equals("6")) {
totalpriceofsandwich = Double.parseDouble(sandwichprice);
} else {
totalpriceofsandwich = 0;
}

} else {
totalpriceofsandwich = 0;
}

/*
* getting the cheese price
* and if that is not null then the totalpriceofcheese
* variable value is changed
*/
price += totalpriceofsandwich;
String cheeseprice = cheeses.checkandgetprice(cheese);
if (cheeseprice != null) {
totalpriceofcheese = Double.parseDouble(cheeseprice);
} else {
totalpriceofcheese = 0;
}

/*
* getting the veggies and if the veggie is counting Onion,Pepper,Tomato
* then the totalpriceofvegi will be increased with that price
*/
price += totalpriceofcheese;
if (veggies.contains("Onion")) {
price += onion.getPrice();
totalpriceofvegi += onion.getPrice();
}

if (veggies.contains("Pepper")) {
price += pepper.getPrice();
totalpriceofvegi += pepper.getPrice();
}

if (veggies.contains("Tomato")) {
price += tomato.getPrice();
totalpriceofvegi += tomato.getPrice();
}

/*
* calculating the final price in each iteration total price will be increased
* by @param price
*/
total += price;

/*
* shows the final order details
*/
JOptionPane.showMessageDialog(frame, " Customer: " + name + "                     Date 2013/05/05 \nPrice of the Sandwich: " + totalpriceofsandwich + "\nPrice of Bread: " + totalpriceofbread + "\nPrice of  Vegetables: " + totalpriceofvegi + " \nPrice for Cheese:" + totalpriceofcheese + "\nTotal Price for Above Items is: " + price, "SUBWAY", JOptionPane.PLAIN_MESSAGE, null);
System.exit(0);

/*
* check whether user has another order if yes then another iteration of this while
* will start and if the input is N then code will exit
*/
if (finish.equals("N")) {
JOptionPane.showMessageDialog(frame, " Customer: " + name + "                  Date 2013/05/05 \nTotal Price is: " + total, "SUBWAY", JOptionPane.PLAIN_MESSAGE, null);
System.exit(0);
}

/*
* setting all the variables to 0 since the orders is now over
*/
price = 0;
totalpriceofvegi = 0;
totalpriceofsandwich = 0;
totalpriceofcheese = 0;
}
}
}

/*
* Sandwich class stores the arraylist of the cart,
* object new set of these values will be created
*/
class Sandwich {

private ArrayList<String> cart;

Sandwich() {
cart = new ArrayList<String>();
sandwichprice = new ArrayList<String>();
sandwichtype = new ArrayList<String>();

}

/**
* This method add sandwich name and the price to
* sandwichprice ArrayList
* @return Nothing
* @param name of the sandwich
* @param price of the sandwich
*/
public void setSandwich(String name, String price) {
}

/**
* @return Nothing
* @param name of the bread
* @param price of the bread
*/
public void setBread(String name, String price) {
}

/**
* This method will set the Type of the bread
* @return Nothing
*/

}

/**
* This method will get the sandwich price
* @return The sandwich price
* @param name sandwich name
*/
public String sandwichcheckgetprice(String name) {
int x = 0;
if (sandwichtype.contains(name)) {
x = sandwichtype.indexOf(name);
return (String) sandwichprice.get(x);
} else {
return null;
}

}

/**
* This method will get the bread price
*/
int x = 0;
} else {
return null;
}

}

/**
* This method add order to cart ArrayList
* @return Nothing
* @param item
*/
}

/**
* This method add type of the sandwich to cart
* @return Nothing
* @param typeofSandwich type of the sandwich
*/
public void SetTypeofSandwich(String typeofSandwich) {
}
}

/*
* Vegetable class is responsible of creating and
* holding veggies
*/
class Vegetables {

private double price;

/**
* This method set the name of the veggie
* @return Nothing
* @param name of the veggie
*/
public void Setname(String name) {
}

/**
* This method set the price of the veggie
* @return Nothing
* @param price veggie price
*/
public void SetPrice(double price) {
this.price = price;
}

/**
* This method get the price of veggie
* @return price
*/
public double getPrice() {
return price;
}
}
/*
* class that stores cheese
*/
class Cheese {

private ArrayList<String> cheesetype, cheeseprice;

Cheese() {
cheesetype = new ArrayList<String>();
cheeseprice = new ArrayList<String>();
}

/**
* This method set the cheese name
* @return nothing
* @param  name cheese name
* @param price cheese price
*/
public void SetCheesename(String name, String price) {
}

/**
* This method will return the cheese price
* @return cheese price
* @param  name cheese name
*/
public String checkandgetprice(String name) {
if (cheesetype.contains(name)) {
int x = cheesetype.indexOf(name);
return (String) cheeseprice.get(x);
} else {
return null;
}
}
}

/*
* create different type of veggies
*/
class Onion extends Vegetables {
}

class Tomato extends Vegetables {
}

class Pepper extends Vegetables {
}

```

Is This A Good Question/Topic? 0

## Replies To: How can I make the final total price look like currency?

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12324
• Posts: 45,424
• Joined: 27-December 08

## Re: How can I make the final total price look like currency?

Posted 05 May 2013 - 07:17 PM

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: How can I make the final total price look like currency?

Posted 05 May 2013 - 07:19 PM

The easiest way is the C like syntax

System.out.printf("\$ %10.2f", value);

where 10 is the number of digits
2 the number of decimals
"f" a floating number

This post has been edited by pbl: 05 May 2013 - 07:20 PM

### #4 g00se

• D.I.C Lover

Reputation: 3562
• Posts: 16,235
• Joined: 20-September 08

## Re: How can I make the final total price look like currency?

Posted 07 May 2013 - 06:28 AM

It's conceivable that there are now, or may be in the future, locales in which currency is not displayed to 2dp [even the dp itself might not be a dot] and/or what precedes the dp is different to the printf approach suggested by pbl. That's why imo mac's approach is better. Where the reverse could be the case is when you want to force a specific display, irrespective of locale. In the latter case
```String s = String.format("%,.2f", doubleValue);

```
will give you a nice display.

### #5 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: How can I make the final total price look like currency?

Posted 07 May 2013 - 03:56 PM

Stop the bad habit of using double and float for monetary values, even for practice applications like this. User big decimal and get a number format currency instance, like Mac said.

### #6 g00se

• D.I.C Lover

Reputation: 3562
• Posts: 16,235
• Joined: 20-September 08

## Re: How can I make the final total price look like currency?

Posted 07 May 2013 - 04:12 PM

Quote

User big decimal and get a number format currency instance, like Mac said.
Bear in mind that if you do use BigDecimal, the NumberFormat is going to turn it into a double anyway to format it

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: How can I make the final total price look like currency?

Posted 07 May 2013 - 05:05 PM

farrell2k, on 07 May 2013 - 06:56 PM, said:

Stop the bad habit of using double and float for monetary values, even for practice applications like this. User big decimal and get a number format currency instance, like Mac said.

Use long and only use cent !!!

### #8 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: How can I make the final total price look like currency?

Posted 07 May 2013 - 05:47 PM

Long is a popular option, too.

### #9 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: How can I make the final total price look like currency?

Posted 07 May 2013 - 09:08 PM

g00se, on 07 May 2013 - 11:12 PM, said:

Quote

User big decimal and get a number format currency instance, like Mac said.
Bear in mind that if you do use BigDecimal, the NumberFormat is going to turn it into a double anyway to format it

Where did you get this idea?

### #10 g00se

• D.I.C Lover

Reputation: 3562
• Posts: 16,235
• Joined: 20-September 08

## Re: How can I make the final total price look like currency?

Posted 08 May 2013 - 02:27 AM

Quote

Where did you get this idea?
Have a guess

### #11 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: How can I make the final total price look like currency?

Posted 08 May 2013 - 10:33 AM

g00se, on 08 May 2013 - 09:27 AM, said:

Quote

Where did you get this idea?
Have a guess />

Smoking too much weed?

### #12 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: How can I make the final total price look like currency?

Posted 08 May 2013 - 10:47 AM

farrell2k, on 08 May 2013 - 01:33 PM, said:

g00se, on 08 May 2013 - 09:27 AM, said:

Quote

Where did you get this idea?
Have a guess />/>

Smoking too much weed? />

API is quite clear

format
public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos)

Formats a number and appends the resulting text to the given string buffer. The number can be of any subclass of Number.
This implementation extracts the number's value using Number.longValue() for all integral type values that can be converted to long without loss of information, including BigInteger values with a bit length of less than 64, and Number.doubleValue() for all other types. It then calls format(long,java.lang.StringBuffer,java.text.FieldPosition) or format(double,java.lang.StringBuffer,java.text.FieldPosition). This may result in loss of magnitude information and precision for BigInteger and BigDecimal values.

### #13 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: How can I make the final total price look like currency?

Posted 08 May 2013 - 12:28 PM

pbl, on 08 May 2013 - 05:47 PM, said:

farrell2k, on 08 May 2013 - 01:33 PM, said:

g00se, on 08 May 2013 - 09:27 AM, said:

Quote

Where did you get this idea?
Have a guess />/>/>

Smoking too much weed? />/>

API is quite clear

format
public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos)

Formats a number and appends the resulting text to the given string buffer. The number can be of any subclass of Number.
This implementation extracts the number's value using Number.longValue() for all integral type values that can be converted to long without loss of information, including BigInteger values with a bit length of less than 64, and Number.doubleValue() for all other types. It then calls format(long,java.lang.StringBuffer,java.text.FieldPosition) or format(double,java.lang.StringBuffer,java.text.FieldPosition). This may result in loss of magnitude information and precision for BigInteger and BigDecimal values.

I gave you a +1 because you worked so hard, but you completely missed the joking nature of my reply.

### #14 g00se

• D.I.C Lover

Reputation: 3562
• Posts: 16,235
• Joined: 20-September 08

## Re: How can I make the final total price look like currency?

Posted 08 May 2013 - 03:13 PM

Quote

I gave you a +1 because you worked so hard, but you completely missed the joking nature of my reply.

No - what pbl was interested in is not your reply but your assertion that BigDecimal + NumberFormat affords greater precision than double + NumberFormat. That was what is important

### #15 farrell2k

• D.I.C Lover

Reputation: 874
• Posts: 2,706
• Joined: 29-July 11

## Re: How can I make the final total price look like currency?

Posted 08 May 2013 - 03:45 PM

g00se, on 08 May 2013 - 10:13 PM, said:

Quote

I gave you a +1 because you worked so hard, but you completely missed the joking nature of my reply. />

No - what pbl was interested in is not your reply but your assertion that BigDecimal + NumberFormat affords greater precision than double + NumberFormat. That was what is important

I figured out before the weed reply, when you told me to guess. I completely admit I was wrong.

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }