| | |
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,642
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
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number object online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string swing template test threads time tree windows working xstream






