944,084 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4881
  • Java RSS
Nov 5th, 2009
0

Trouble converting int to float

Expand Post »
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?

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

Java Syntax (Toggle Plain Text)
  1. System.out.println("average(\"7, 5\") = " + Lab7.average(7, 5));

RESTULTS:
average("7, 5") = 6.0
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bluesilver is offline Offline
15 posts
since Oct 2009
Nov 5th, 2009
0
Re: Trouble converting int to float
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 5th, 2009
0
Re: Trouble converting int to float
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bluesilver is offline Offline
15 posts
since Oct 2009
Nov 5th, 2009
0
Re: Trouble converting int to float
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 5th, 2009
0
Re: Trouble converting int to float
Are you expecting it to print the "f"? Doubles and floats don't look any different as output.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 5th, 2009
0
Re: Trouble converting int to float
Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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".
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bluesilver is offline Offline
15 posts
since Oct 2009
Nov 5th, 2009
0
Re: Trouble converting int to float
Then do System.out.println("f") afterwards. lol.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 5th, 2009
0
Re: Trouble converting int to float
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 5th, 2009
0
Re: Trouble converting int to float
Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bluesilver is offline Offline
15 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Crisis with co2 footprint program
Next Thread in Java Forum Timeline: HELP!!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC