Results 1 to 6 of 6
  1. #1
    Moustafa taha is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default Format the output

    This code compares loan with various interest rates
    Java Code:
     public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
            //Prompt the user to enter the loan amount
            System.out.print("Enter the loan amount:");
            int loanAmount=input.nextInt();
            //Promp the user to enter the period in number of years
            System.out.print("Enter number of years:");
            int numberOfYears=input.nextInt();
            System.out.println("Interest Rate   Monthly Payment   Total Payment");
            int months=numberOfYears*12;
            double interestRate=5;
            double monthlyPayment;
            double rate;
            double totalPayments;
            while(interestRate<=8)
            {
                rate=interestRate/1200;
                //Calculating monthly payment
                 monthlyPayment=(rate+(rate/((Math.pow((1+rate),months))-1)))*loanAmount;
                //Calculating total payments
                 totalPayments=monthlyPayment*months;
                System.out.printf("%-13f   %-15.2f   %-13.2f\n",interestRate,monthlyPayment,totalPayments);
                interestRate+=(double)1/8;
            }
        }
    The problem in a small thing in the format of the output
    the first column of the output is as follows
    5.000000
    5.125000
    5.250000
    5.375000
    5.500000
    5.625000
    5.750000
    5.875000
    6.000000
    6.125000
    6.250000
    6.375000
    6.500000
    6.625000
    6.750000
    7.000000
    7.125000
    7.250000
    7.375000
    7.625000
    7.750000
    7.875000
    8.000000
    i want to remove zeros and make the format like this
    5%
    5.125%
    5.25%
    ....
    ....
    8.0%
    thanks

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Firstly, can we see what you've tried? Have you tried DecimalFormat or other printf methods?

    Also, do you want 5% or 5.0%? You use both ambiguously in your example.

  3. #3
    Moustafa taha is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    I've tried printf method as following
    System.out.printf("%-13f %-15.2f %-13.2f\n",interestRate,monthlyPayment,totalPayments );
    and i want the format to be like 5%

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Have a look at: DecimalFormat (Java 2 Platform SE v1.4.2)

    Then you will have something like this:
    Java Code:
    System.out.printf("%-13s   %-15.2f   %-13.2f\n",new DecimalFormat(YourDecimalFormatWillGoHere).format(interestRate),monthlyPayment,totalPayments);
    With the API documentation you should be able to figure out what "YourDecimalFormatWillGoHere" will be.

  5. #5
    Moustafa taha is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks very much for your help .
    after I've read the API documentation i understood little things and tried to improve this line as follow
    Java Code:
    System.out.printf("%-13s   %-15.2f   %-13.2f\n",new DecimalFormat("0.###%").format(interestRate),monthlyPayment,totalPayments);
    but as you know the % sign multiply the number by 100 and show as percentage
    and this is the new problem.......after this formatting the column is as follows
    500%
    512.5%
    525%
    537.5%
    550%
    562.5%
    575%
    587.5%
    600%
    612.5%
    625%
    637.5%
    650%
    662.5%
    675%
    687.5%
    700%
    712.5%
    725%
    737.5%
    750%
    762.5%
    775%
    787.5%
    800%
    so I've to multiply the output by 100 to get true result
    How can i do this Particularly i deal with a string?
    I hope replying me.
    thanks

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by API
    '
    Prefix or suffix
    No
    Used to quote special characters in a prefix or suffix, for example, "'#'#" formats 123 to "#123". To create a single quote itself, use two in a row: "# o''clock".
    That's directly from the API, describing how to use literals in your string.

Similar Threads

  1. Replies: 0
    Last Post: 09-30-2010, 09:16 PM
  2. output for list of names in a format
    By Ms.Ranjan in forum New To Java
    Replies: 7
    Last Post: 06-18-2009, 05:47 PM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  4. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 11:21 AM
  5. Format
    By 2ndis1stplaceloser in forum New To Java
    Replies: 2
    Last Post: 10-21-2008, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •