views:

2752

answers:

3

This is one of those silly intro questions, but how does one convert from an int or a decimal to a float in C#. I need to use a float for a third-party control but I don't use them in my code and I'm not sure how to end up with a float.

+6  A: 

You can just do a cast

int val1 = 1;
float val2 = (float)val1;

or

decimal val3 = 3;
float val4 = (float)val3;
heavyd
+2  A: 

isn't it just...

float f = (float)6;
nedlud
+2  A: 

The same as an int:

float f = 6;

Also here's how to programatically convert from an int to a float, and a single in C# is the same as a float:

int i = 8;
float f = Convert.ToSingle(i);

Or you can just cast an int to a float:

float f = (float)i;
Chris Pietschmann