Trouble converting int to float

Thread Solved

Join Date: Oct 2009
Posts: 15
Reputation: Bluesilver is an unknown quantity at this point 
Solved Threads: 0
Bluesilver Bluesilver is offline Offline
Newbie Poster

Trouble converting int to float

 
0
  #1
19 Days Ago
I am trying to write a method that accepts two integer parameters and returns their average as a float, but for some reason it is only returning a double. I am quite sure that there needs to be "f" after the resulting value, but it's not showing up. Can someone please help me fix this?

  1. public static float average(int x, int y)
  2. {
  3. return (x + y)/2;
  4. }

  1. System.out.println("average(\"7, 5\") = " + Lab7.average(7, 5));

RESTULTS:
average("7, 5") = 6.0
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
19 Days Ago
  1. return (float)((x+y)/2.0);
  2.  
  3. OR
  4.  
  5. return ((float)(x+y))/2.0;
Last edited by BestJewSinceJC; 19 Days Ago at 7:40 pm.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: Bluesilver is an unknown quantity at this point 
Solved Threads: 0
Bluesilver Bluesilver is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
Originally Posted by BestJewSinceJC View Post
  1. return (float)((x+y)/2.0);
  2.  
  3. OR
  4.  
  5. return ((float)(x+y))/2.0;
I tried both suggestions but my results are still only double... is there some other way of converting an int to a float? I tried casting too, but it didn't work either. Perhaps I'm doing something completely unrelated wrong?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #4
19 Days Ago
You're probably storing it in a double variable. For example

double result = 0.0;

result = average(x,y);
Now result is a double which is a larger type than float.
Out.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,433
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #5
19 Days Ago
Are you expecting it to print the "f"? Doubles and floats don't look any different as output.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: Bluesilver is an unknown quantity at this point 
Solved Threads: 0
Bluesilver Bluesilver is offline Offline
Newbie Poster
 
0
  #6
19 Days Ago
Originally Posted by Ezzaral View Post
Are you expecting it to print the "f"? Doubles and floats don't look any different as output.
Well if you're right, then I suppose the assignment will be a lot easier to complete... but I'm quite sure my instructor said that the printed value of a float was supposed to be followed by an "f".
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #7
19 Days Ago
Then do System.out.println("f") afterwards. lol.
Out.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,433
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #8
19 Days Ago
The literal value of a float in your code would be "6.0f", but printed output does not show that "f" unless you put it there explicitly.

Your output is perfectly fine.
Last edited by Ezzaral; 19 Days Ago at 10:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: Bluesilver is an unknown quantity at this point 
Solved Threads: 0
Bluesilver Bluesilver is offline Offline
Newbie Poster
 
0
  #9
19 Days Ago
Originally Posted by Ezzaral View Post
The literal value of a float in your code would be "6.0f", but printed output does not show that "f" unless you put it there explicitly.

Your output is perfectly fine.
Ok I'm really glad that's the case, because I had spent hours trying to figure out what I was doing wrong lol. Thank you very much for your time!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC