This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In an effort to help you get the most from our forums, we've compiled a list of tips for asking questions here. You can find the list in our FAQ section here. In particular, please see CarefullyChooseOneForum.
I'll move this thread to the Java In General (Beginner) forum for you. Again, welcome to JavaRanch and good luck with your question. -Ben
It all depends whether you are using J5 or not. In J5, you can use the % tags, which you will find in the API description of the Formatter class. For example %.2f will print a floating-point number with two figures after the decimal point.
Campbell! Thank you for your suggestion. I am utilizing JDK 1.4.1 in this project. I have tried using NumberFormat and DecimalFormat classes which you mentioned but could not be able to get desired results. Please explain clearly on how to apply these classes to my code. Thanks a lot.
A DecimalFormat object is not something that represents a number. If x1 and x2 are doubles, and x3 is a DecimalFormat object as in your code above, the line:
x3 = (x1 + x2);
will ofcourse not work. The DecimalFormat object is an object that you use to format a number (in a variable of type double, for example) into text. To do that, you use the format(...) method of the object.