File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes beginner precision question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginner precision question" Watch "beginner precision question" New topic
Author

beginner precision question

Tod Novak
Greenhorn

Joined: Dec 14, 2006
Posts: 13

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.


"-Of all the things I've lost...I miss my mind the most!"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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 ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40032
    
  28
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

Joined: Dec 14, 2006
Posts: 13

Thanks all.

Got some reading to do

Appreciate the help.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3274
I would prefer using a DecimalFormat class with Log4J.


[ May 15, 2008: Message edited by: arulk pillai ]

500+ Java Interview Questions and Answers | Java job hunting know how & Java resumes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: beginner precision question