Trouble converting int to float

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
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
Nov 5th, 2009
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,638
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso
 
0
  #2
Nov 5th, 2009
  1. return (float)((x+y)/2.0);
  2.  
  3. OR
  4.  
  5. return ((float)(x+y))/2.0;
Last edited by BestJewSinceJC; Nov 5th, 2009 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
Nov 5th, 2009
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,638
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso
 
0
  #4
Nov 5th, 2009
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,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #5
Nov 5th, 2009
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
Nov 5th, 2009
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,638
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso
 
0
  #7
Nov 5th, 2009
Then do System.out.println("f") afterwards. lol.
Out.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #8
Nov 5th, 2009
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; Nov 5th, 2009 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
Nov 5th, 2009
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



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC