Solved

Java Possible loss of Precision ????

Posted on 2012-12-27
1
483 Views
Last Modified: 2012-12-27
Hello

Is there anyway I can fix the 'possible loss of precision' error in my Java program below without changing the int value 'start' to a double?

/*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. Your program should also display the total interest
earned at the end of 15 years.*/


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

public class Q4_Int_Double
{
	public static void main (String [] args)
	{
		int start = 20;
		double interest = 0.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
Comment
Question by:Kevin_Coors
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 255 total points
ID: 38724191
Not every integer divides another integer into equal parts, correct? This means that when you divide two arbitrary numbers, depending on the numbers there is a chance that a fractional part will remain (e.g. 3 / 2). When you divide two integers in Java, any fractional part is truncated off. This is what the error message is telling you. You need to account for this by not dividing two integers. Rather you need to divide with at least one floating point (fractional) value. Can you think of a way to turn either start or 100 into a floating point value?

Hint:

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 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