| | |
Trouble converting int to float
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 15
Reputation:
Solved Threads: 0
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?
RESTULTS:
average("7, 5") = 6.0
Java Syntax (Toggle Plain Text)
public static float average(int x, int y) { return (x + y)/2; }
Java Syntax (Toggle Plain Text)
System.out.println("average(\"7, 5\") = " + Lab7.average(7, 5));
RESTULTS:
average("7, 5") = 6.0
•
•
Join Date: Sep 2008
Posts: 1,620
Reputation:
Solved Threads: 205
0
#2 Nov 5th, 2009
Java Syntax (Toggle Plain Text)
return (float)((x+y)/2.0); OR return ((float)(x+y))/2.0;
Last edited by BestJewSinceJC; Nov 5th, 2009 at 7:40 pm.
Out.
•
•
Join Date: Oct 2009
Posts: 15
Reputation:
Solved Threads: 0
0
#3 Nov 5th, 2009
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?
•
•
Join Date: Oct 2009
Posts: 15
Reputation:
Solved Threads: 0
0
#6 Nov 5th, 2009
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".
•
•
Join Date: Oct 2009
Posts: 15
Reputation:
Solved Threads: 0
0
#9 Nov 5th, 2009
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!
![]() |
Similar Threads
- Allegro 4.2.2 problems (C++)
- converting from string array to int array (C++)
- Help with converting a string to a float then multiplying the answer (C++)
- Weird fstream problem when access member of a object inside other class.. (C++)
- C++: problam with data type(int,float,double) (C++)
- Converting char into int (C++)
Other Threads in the Java Forum
- Previous Thread: Crisis with co2 footprint program
- Next Thread: HELP!!!!!
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows






