hello every one
this program compares loans with various interest rates

``````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;
}
}

}``````

I've used here the printf method in this line

``System.out.printf("%-13f   %-15.2f   %-13.2f\n",interestRate,monthlyPayment,totalPayments);``

the problem in the format of this first column of the output
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
Contributors
1
2
Views
7 Years
Discussion Span
Last Post by Dupron

You can solve your problem using the format method and restrict your decimal places upto which digit you want.
For eg:
To restrict to 3 digits use

``````String s=String.format("%.3f",4765.098000);
System.out.println(s);``````

It will give output
4765.098
i.e upto 3 decimal places.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.