• 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

beginner precision question

 
Greenhorn
Posts: 13
MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is just a sample of what I'm trying to get a grasp on.

I'm trying to figure out how to get both places to display after the decimal when both are zeros.



Again, this is just to illustrate.

If I input 1000 ($10.00) as price, I would expect to get back 12.00 as the result, not 12.0

Thanks in advance.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use System.out.printf instead of System.out.println. Check PrintWriter.printf for more information; keep in mind it does NOT print an enter unless you tell it to.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the JavaDoc for the DecimalFormat class. It is a class used to format Decimal numbers like you are trying to do. The DecimalFormat class is a subclass of the NumberFormat.

You can then look at Java Tutorial on Formatting Numeric Output. That lesson first talks about the System class' printf() and format() methods, and then the DecimalFormat class.

You can also find examples of using the DecimalFormat class on exampledepot.com
[ May 15, 2008: Message edited by: Mark Vedder ]
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Rob Prime. More details about the tags with % in the Formatter clas API documentation. Use %n instead of \n after printf to go onto a new line.
 
Tod Novak
Greenhorn
Posts: 13
MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all.

Got some reading to do

Appreciate the help.
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would prefer using a DecimalFormat class with Log4J.


[ May 15, 2008: Message edited by: arulk pillai ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic