Link to home
Start Free TrialLog in
Avatar of Kevin_Coors
Kevin_Coors

asked on

Rounding off Java numbers

Hello

Could someone please run my programme below and tell me how I can round off the double values to 2 decimal places

Many thanks

/*4. Write a program to calculate and display the amount of money in a bank
account that was opened with a deposit of €20 fifteen years ago and has
earned interest at 10% (compound interest) per annum. Your program
should display the amount of money in the account at the end of each year
for all of the 15 years.*/

public class Q4
{
	public static void main (String [] args)
	{
		double start = 20;
		double years = 15;
		double interest = 10;
		double totalInt;
		
		for (int counter = 1; counter <=15; counter ++)
		{
		
		totalInt = (start / 100) * interest;
		start = start + totalInt;
						
		System.out.println("Year " + counter + " Total €" + start);
		
		}		
       }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kevin_Coors
Kevin_Coors

ASKER

Thanks I have never used the Java Api before as I am a novice.

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Q4
{
	public static void main (String [] args)
	{
		double start = 20;
		double years = 15;
		double interest = 10;
		double totalInt;
		
		NumberFormat formatter = new DecimalFormat("0.00");
		
		for (int counter = 1; counter <=15; counter ++)
		{
		
		totalInt = (start / 100) * interest;
		start = start + totalInt;
						
		System.out.println("Year " + counter + " Total €" + formatter.format(start));
		
		}		

	}
}

Open in new window