# Int to Float

This is a discussion on Int to Float within the C Programming forums, part of the General Programming Boards category; I've tried to do this: Code: #include <stdio.h> int main() { int a=3,b=2; float c; c = (float)(a/b); printf("%f\n",c); return ...

1. ## Int to Float

I've tried to do this:

Code:
```#include <stdio.h>

int main() {
int a=3,b=2;
float c;

c = (float)(a/b);
printf("%f\n",c);

return 0;
}```
But instead of 3/2 he returns 1.00000000...

How can i convert Int to float?

Thanks!

2. Make sure at least one of the operands is converted to float, and the expression will be evaluated properly:
Code:
`c = (float)a/b;`
What you have presently is an integer operation that's cast to float. 3/2 is 1 (note that both sides are integers), and converted to float it's 1.0.

3. is there any error here:

Code:
```xmax = (((float)x1 + ((float)wire_data[0] /2)) > xmax) ? ((float)x1 + ((float)wire_data[0] /2)) : xmax;

xmin = (((float)x1 - ((float)wire_data[0] /2)) < xmin) ? ((float)x1 - ((float)wire_data[0] /2)) : xmin;
ymax = (((float)y1 + ((float)wire_data[0] /2)) > ymax) ? ((float)y1 + ((float)wire_data[0] /2)) : ymax;

ymin = (((float)y1 - ((float)wire_data[0] /2)) < ymin) ? ((float)y1 - ((float)wire_data[0] /2)) : ymin;```

4. Originally Posted by Milhas
is there any error here:
I think it's okay.

5. If wire_data[0] is an int, and you want the result of the divide by 2 to be a float, you could save a few keystrokes and simply:
Code:
`wire_data[0] /2.0`
In this case, the 2.0 is a floating point value (a double), but you could also code
Code:
`wire_data[0] /2.0f`
if you really wanted a float and not a double.

Todd

6. Hey guys, sorry to bring back an old thread but I'd rather do this than start a new one!

I'm basically having trouble converting an int to a float. I've tried the following code and ever single time floatadd = 1077280768
Code:
`floatadd = newadd;`
Code:
`floatadd = (double)newadd;`
Code:
`floatadd = double(newadd);`
I.e I've tried every option I've seen listed around here. Newadd is 22 and I need to convert it to a floating point to do some jazz on it, but all the above give 1077280768 as an answer. Any ideas?

I'm sorry if this is a noobie question, I am very new to C programming.

Thanks,

pgcrooks

7. Originally Posted by pgcrooks
I.e I've tried every option I've seen listed around here. Newadd is 22 and I need to convert it to a floating point to do some jazz on it, but all the above give 1077280768 as an answer. Any ideas?
float and double are two different things. The below code will work:
Code:
```float myfloat = (float) newadd;

printf ("myfloat = &#37;f\n", myfloat);```
QuantumPete

8. Thanks Pete, I realise float and double are different, I just copied and pasted the same thing 3 times above. Found the problem, turns out I'm a complete idiot and used %d in the printf command. Oh well... Stared at the convert line for ages without ever looking at the printf. Sorry!

9. 1. This probably SHOULD be a new thread.
2. You will need to give us a bit more code. One very likely scenario is that you are outputting a floating point number as an integer. The 1077280768 value when it turns to hex is 0x40360000, which looks suspiciously like a floating point number to me [yes, I know, I'm a geek by being able to "spot" floating point numbers in hex, but so be it].

--
Mats