Solved

Rounding off Java numbers

Posted on 2012-12-27
2
365 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 40
Java SE 8u111  Lot of stuff broke 11 52
nextBoolean(double p) for Random class 3 35
Problem to open text file 11 68
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now