Results 1 to 6 of 6
Thread: Format the output
- 10-17-2010, 02:34 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
Format the output
This code compares loan with various interest rates
The problem in a small thing in the format of the outputJava 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 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 #2
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 #3
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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 #4
Have a look at: DecimalFormat (Java 2 Platform SE v1.4.2)
Then you will have something like this:
With the API documentation you should be able to figure out what "YourDecimalFormatWillGoHere" will be.Java Code:System.out.printf("%-13s %-15.2f %-13.2f\n",new DecimalFormat(YourDecimalFormatWillGoHere).format(interestRate),monthlyPayment,totalPayments);
- 10-17-2010, 11:26 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 4
- Rep Power
- 0
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
but as you know the % sign multiply the number by 100 and show as percentageJava Code:System.out.printf("%-13s %-15.2f %-13.2f\n",new DecimalFormat("0.###%").format(interestRate),monthlyPayment,totalPayments);
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
- 10-18-2010, 05:01 AM #6
That's directly from the API, describing how to use literals in your string.
Originally Posted by API
Similar Threads
-
how to get the resultset output into an output file
By renu in forum New To JavaReplies: 0Last Post: 09-30-2010, 08:16 PM -
output for list of names in a format
By Ms.Ranjan in forum New To JavaReplies: 7Last Post: 06-18-2009, 04:47 PM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
how to convert one format to another format
By mahipal_reddy621 in forum New To JavaReplies: 1Last Post: 12-02-2008, 10:21 AM -
Format
By 2ndis1stplaceloser in forum New To JavaReplies: 2Last Post: 10-21-2008, 06:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks