• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Help with DecimalFormat

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line you are getting the error? Also please use code tag.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nvm got it working just fine, thank you very much for all your help both of you. Much appreciated =)
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic