General discussion

Locked

Convert Integer to Float in C

By naren1983 ·
I want to convert an integer value into a float value in Turbo C. A friend told me that there is possibility of convert Float into int. Any one help me.

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by carlos.nino In reply to Convert Integer to Float ...

Check theese sites:

http://www.control.com/1026179560/index_html

www.al-williams.com/pak1.pdf

http://cvsup.pt.freebsd.org/cgi-bin/cvsweb/cvsweb.cgi/xsrc/xc/lib/Xt/Converters.c?cvsroot=NetBSD&rev=1.1.1.3

Good luck!!

Collapse -

by awimsatt In reply to Convert Integer to Float ...

I believe the two functions you want are itof() and ftoi() ( read I-to-F or Int to Float ). It's been a while since I've coded C though.

Collapse -

by csmith In reply to Convert Integer to Float ...

From your question I could not glean enough information about the application to figure out which coversion you need.
Is "Type Promotion" applicable?
Type promotion is where you have mixed data. (In your case float and integer) and you want to add them.
//Example 1:

int i;
float f;
float sum;

sum = (f + i) ;


Or do you need a "Cast" construct?
Casts are used to force an expression to be a specific type.
//Example 2:

int x , y ;
float fraction ;

fraction = (float) x / y ;

Regards, Chris

Collapse -

by somu_rec In reply to Convert Integer to Float ...

Type casting is the best way for compatible data types like int,float turbo c normaly does automatic type casting during the execution of statements
ex if you have a computing stament involving int and float data types int a+float b the resulting answer will only be an int and not an float

explicit type casting can also be done
example
float a;
int b;
(float)b will give an float value of the int b and
(int)a will give an int value of the float a

Collapse -

by kc_gopal In reply to Convert Integer to Float ...

Type casting seems to be the best option for you. You can either declare a float variable and assign the int value to it or use the casting operator to convert the int value for the operation.
Method 1

int i;
float f;
i = 10;
f = i;


Method 2
int i;
(float)i

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums