Results 1 to 2 of 2
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default arithmetic with doubles problem

    hello so i just wrote this code

    Java Code:
    public static void main(String[] args) {
    		
    		int[] meals = {0,1,1,1,1};
    		double cost = 0.0;
    		
    		int i =0;
    		while (i==0)
    		{
    			if (meals[i] == 1 )
    			     cost += 1.75;
    			      else if (meals[i] == 2)
    			     cost  += 3.83;
    			      else if (meals[i] == 3)
    			    cost += 5.58;
    			      else if (meals[i] == 4)
    			    cost += 7.66;
    			      else if (meals[i] == 0)
    			    	  cost += 0;
    			      else 
    			   cost += 9.41;
    			i++;
    		}
    		while (i==1)
    			{
    				if (meals[i] == 1 )
    				     cost += 1.75;
    				      else if (meals[i] == 2)
    				     cost  += 2*1.75;
    				      else if (meals[i] == 3)
    				    cost += 1.75 * 3;
    				      else if (meals[i] == 4)
    				    cost += 1.75 * 4;
    				      else if (meals[i] == 0)
    				    	  cost += 0;
    				      else 
    				   cost += 1.75 * 5;
    				i++;
    			}
    		while (i==2)
    		{
    			if (meals[i] == 1 )
    			     cost += 2.08;
    			      else if (meals[i] == 2)
    			     cost  += 2*2.08;
    			      else if (meals[i] == 3)
    			    cost += 2.08 * 3;
    			      else if (meals[i] == 4)
    			    cost += 2.08 * 4;
    			      else if (meals[i] == 0)
    			    	  cost += 0;
    			      else 
    			   cost += 2.08 * 5;
    			i++;
    		}
    		while (i==3)
    		{
    			if (meals[i] == 1 )
    			     cost += 2.14;
    			      else if (meals[i] == 2)
    			     cost  += 2*2.14;
    			      else if (meals[i] == 3)
    			    cost += 2.14 * 3;
    			      else if (meals[i] == 4)
    			    cost += 2.14 * 4;
    			      else if (meals[i] == 0)
    			    	  cost += 0;
    			      else 
    			   cost +=2.14 * 5;
    			i++;
    		}
    			while (i==4)
    			{
    				if (meals[i] == 1 )
    				     cost += 0.98;
    				      else if (meals[i] == 2)
    				     cost  += 2*0.98;
    				      else if (meals[i] == 3)
    				    cost += 0.98 * 3;
    				      else if (meals[i] == 4)
    				    cost += 0.98 * 4;
    				      else if (meals[i] == 0)
    				    	  cost += 0;
    				      else 
    				   cost +=0.98 * 5;
    				i++;
    			
    			}
    			System.out.print(cost);
    	}
    
    }
    and the result i get it is 6.950000000000001
    instead of 6.95, is there a way that i can get it to 6.95 without decimal formating?
    Last edited by Fubarable; 10-04-2010 at 03:20 AM. Reason: bottom code tag corrected

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

Similar Threads

  1. Cant use doubles
    By Freakzoyd in forum New To Java
    Replies: 27
    Last Post: 07-24-2010, 04:48 AM
  2. Weird problem with doubles not being added to sum
    By kiregad in forum New To Java
    Replies: 3
    Last Post: 03-24-2010, 09:08 PM
  3. arrays using doubles [SOLVED]
    By soccer_kid_6 in forum New To Java
    Replies: 0
    Last Post: 03-10-2010, 01:56 AM
  4. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •