| | |
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,638
Reputation:
Solved Threads: 206
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 |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation banking binary bluetooth character chat class classes client code component constructor crashcourse database design developmenthelp draw eclipse error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide if_statement image input integer interface j2me java javaarraylist javadoc javaee javamicroeditionuseofmotionsensor javaprojects jetbrains jmf jni jpanel julia linux list loop map method methods mobile multithreading netbeans newbie number object oracle pearl print printing problem program programming project recursion remove scanner screen server set size sms socket software sort splash sql stop string swing test textfield thread threads time tree validation windows






