Solved

Rounding off Java numbers

Posted on 2012-12-27
2
368 Views
Last Modified: 2012-12-27
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

0
Comment
Question by:Kevin_Coors
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 38724033
Have a look at the DecimalFormat class.
0
 

Author Comment

by:Kevin_Coors
ID: 38724094
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

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web application structure 18 101
Custom Java Application connects to database: Which one of the 20? 4 35
java imports not found 4 29
swing controls 2 16
A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question