Search This Blog

Friday, October 25, 2013

Float type in C#

C# > Types > Float

Float is a simple type that stores 32-bit floating-point values.

By default, a float on the right side of the assignment operator is treated as double.
If you do not use the suffix F, you will get a compilation error because you are trying to store a double value into a float variable:

float x = 7.5; // Error Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type

To initialize a float variable, use the suffix f or F:

float x = 7.5F; // Ok