DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2004
    Posts
    46

    Amending simple program

    I have this very simple program which calculates and displays the price of a product after sales tax has been added.


    Code:
    public class FindCost
    {
      public static void main (String[] args )
    {
      double price, tax;
      System.out.println("*** Product Price Check ***");
      System.out.print("Enter initial price: "); //prompt for input
      price = EasyIn.getDouble(); //input method called
      System.out.print("Enter tax rate: "); //prompt for input
      tax = EasyIn.getDouble(); //input method called
      price = price * (1 + tax/100);
      System.out.println("Cost after tax = " + price);
     }
    }

    I want to ammend the program so that:
    - all currency values are displayed to two decimal places;
    - line and tab spaces are used to improve the layout of the information displayed

    Whats the best way to do this in the code?

    Thanks

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    import java.text.*;

    then make a DecimalFormat instance:

    DecimalFormat twoDecimals=new DecimalFormat ("0.00");

    then when you have a double value you write it like:

    System.out.print(twoDecimals.format(theDoubleValue));

    And for stating the obvious:

    For newlines in a string, use '\n', for tabs use '\t';
    eschew obfuscation

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links