Solved

Java Possible loss of Precision ????

Posted on 2012-12-27
1
477 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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
How to configure empty element in XML Document parser? 15 45
Notify sent to other threads in Java 9 43
Java program running SQL query 5 50
Programming Codes 2 45
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 …
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 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