Welcome to Dream.In.Code
Become a Java Expert!

Join 150,384 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,143 people online right now. Registration is fast and FREE... Join Now!




possible loss of precision

 
Reply to this topicStart new topic

possible loss of precision

jblesko
19 Feb, 2008 - 11:39 AM
Post #1

New D.I.C Head
*

Joined: 19 Feb, 2008
Posts: 1

CODE
import javax.swing.JOptionPane;

public class NO1HW4 {
    
    public static void main(String[] args) {
        
        String firstNumber;
        int number1;
        int total;
        int GrandTotal;
                
        GrandTotal = 0;
        number1 = 0;
        total = 0;
                       
    while ( number1 != -1 )
    {
    
            firstNumber = JOptionPane.showInputDialog( "Enter Cost of Sale or -1 to End:" );
            
            number1 = Integer.parseInt( firstNumber );
            total = total + number1;
    }    
        
        GrandTotal = total * 0.09;
        GrandTotal = GrandTotal + 200;
            
        JOptionPane.showMessageDialog( null, "Weekly earnings = $" + GrandTotal,
         "Results", JOptionPane.PLAIN_MESSAGE );
        
        
    }
}

User is offlineProfile CardPM
+Quote Post

AbuJaFaR
RE: Possible Loss Of Precision
19 Feb, 2008 - 01:01 PM
Post #2

D.I.C Head
**

Joined: 13 Dec, 2007
Posts: 123


My Contributions
Hello there,
Please edit your post and put your code inside
the code brackets.Thnx.
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Possible Loss Of Precision
19 Feb, 2008 - 02:45 PM
Post #3

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
The variable GrandTotal needs to be a double or a float in order to calculate GT = total *0.09
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Possible Loss Of Precision
19 Feb, 2008 - 02:47 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You are getting the "loss of precision" because you have declared GrandTotal as an integer type. But you are trying to store the value of a double in this line of code, GrandTotal = total * 0.09;. Integer data types don't have decimal values, therefore you will lose some of the value when multiplying 0.09 * total.

Change the data type to a double.
CODE

double GrandTotal;

User is offlineProfile CardPM
+Quote Post

bhandari
RE: Possible Loss Of Precision
20 Feb, 2008 - 12:28 AM
Post #5

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
Change the data type to a double.
CODE
double GrandTotal;


or make an explicit cast as
CODE
GrandTotal = (int) (total * 0.09);

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:41PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month