| | |
Trouble converting int to float
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,563
Reputation:
Solved Threads: 196
0
#2 19 Days Ago
Java Syntax (Toggle Plain Text)
return (float)((x+y)/2.0); OR return ((float)(x+y))/2.0;
Last edited by BestJewSinceJC; 19 Days Ago at 7:40 pm.
Out.
•
•
Join Date: Oct 2009
Posts: 15
Reputation:
Solved Threads: 0
0
#3 19 Days Ago
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 19 Days Ago
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 19 Days Ago
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 |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program programming project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows






