Solved

Java Possible loss of Precision ????

Posted on 2012-12-27
1
469 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
1 Comment
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 255 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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 …

771 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

10 Experts available now in Live!

Get 1:1 Help Now