[c#] How do I convert Int/Decimal to float in C#?

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.

This question is related to c# casting floating-point

The answer is


The same as an int:

float f = 6;

Also here's how to programmatically 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;

It is just:

float f = (float)6;

You don't even need to cast, it is implicit.

int i = 3;

float f = i;

A full list/table of implicit numeric conversions can be seen here http://msdn.microsoft.com/en-us/library/y5b434w4.aspx