# Format the output

• 10-17-2010, 02:34 AM
Moustafa taha
Format the output
This code compares loan with various interest rates
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
• 10-17-2010, 03:40 AM
Zack
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.
• 10-17-2010, 11:58 AM
Moustafa taha
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%
• 10-17-2010, 07:42 PM
Zack
Have a look at: DecimalFormat (Java 2 Platform SE v1.4.2)

Then you will have something like this:
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.
• 10-17-2010, 11:26 PM
Moustafa taha
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
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?