• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Help with DecimalFormat  RSS feed

 
tom bull
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm new to programming and I have an assignment to create a JFrame pizzaorder and at the end of this order after the totalprice is calculated I need to format the number to ##.## but I keep getting a cannot find symbol error, any help would be appreciated.

Source Code:


 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line you are getting the error? Also please use code tag.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

As Tushar has already said, it helps a lot if you UseCodeTags when posting code. I've added them to your post for you this time. But click that link to find out how to do it yourself (it's not that hard)


The error I get when copy/pasting your code is actually for the java.text.DecimalFormat class. (line 165)
I think all you need to do to resolve that error is import it at the top, and you should be good to go.

 
tom bull
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Tushar and Stefan I will use codetags next time. That fixes the problem and allows me to use the DecimalFormat but when the totalprice is coming back to me after I've clicked a radio button its coming back as say 11.0 and I need it to come back such as $11.00 can you point me in the right direction to fix this issue please?
 
tom bull
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nvm got it working just fine, thank you very much for all your help both of you. Much appreciated =)
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Please tell us how you correted the problem. Why are you using decimal format when you could use the % tags. Look in the Java® Tutorials and use ctrl‑F‑“formatting” and you probably get two hits. Read both.
String output = String.format("$%6.2f", dollars);
 
tom bull
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well my main issue was using the DecimalFormat which thanks to Stefan I was easily able to fix after that my program worked fine I just needed the output to show the correct decimal place.


Which in my original code I had as:

totalprice = toppingprice + sizeprice;

DecimalFormat df = new DecimalFormat("0,000,000,000");
System.out.println(df.format(totalprice));


I just needed to format my numbers properly in the new code:

totalprice = toppingprice + sizeprice;

DecimalFormat df = new DecimalFormat("$.00");
price.setText("Price of your pizza is " + (df.format(totalprice)));

}

Overall it was an easy fix, thanks for all your assistance guys, also I was using DecimalFormat as opposed to % tags because this is how my professor requested we format the prices. It was an assignment.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!