Value Float

Float is a floating-point number. The C# language provides this type as a single-precision floating point number representation. Float is less precise than a double. It occupies four bytes and is no larger than an int.
KeywordsThis C# page shows the float number type. Float is a single-precision floating point number representation.

To begin, you will need to use the character suffix f or F on constants you want to be treated as float values. If you don't specify f or F, you will get a compile-time error. The C# compiler treats these values as doubles by default. This program shows how you can specify floats with f or F. It is often preferred to use the uppercase F to reduce possible confusion because the characters are less similar to the digits.
SuffixProgram that uses float suffixes [C#]
using System;
class Program
{
static void Main()
{
float val1 = 1.001f; // Lowercase f suffix.
float val2 = 1.002F; // Uppercase F suffix.
Console.WriteLine(val1);
Console.WriteLine(val2);
}
}
Output
1.001
1.002The float type can be used in the same ways that other numeric types in the C# language can be used. You can convert floats to strings, either with ToString or through Console.WriteLine. You can assign negative floating point numbers to floats. You can use the == equality operator on floats, and also other arithmetic operators.
Program that tests float type [C#]
using System;
class Program
{
static void Main()
{
// Use float type.
float number = 1.5F;
Console.WriteLine(number);
// Set to negative value.
number = -1.001F;
Console.WriteLine(number);
Console.WriteLine(number == -1.001F); // Use == operator
Console.WriteLine(number + 200); // Use + operator
Console.WriteLine(number.GetType());
Console.WriteLine(typeof(float));
Console.WriteLine(float.MinValue.ToString("0"));
Console.WriteLine(float.MaxValue.ToString("0"));
// Find the memory usage for a float value.
long bytes1 = GC.GetTotalMemory(false);
float[] array = new float[1000 * 1000];
array[0] = 1;
long bytes2 = GC.GetTotalMemory(false);
Console.WriteLine("{0} bytes per float", ((bytes2 - bytes1) / (1000000)));
}
}
Output
1.5
-1.001
True
198.999
System.Single
System.Single
-340282300000000000000000000000000000000
340282300000000000000000000000000000000
4 bytes per float
Typeof float. The typeof(float) expression in the program, as well as the GetType method, both return the System.Single type. The C# language aliases the float keyword to System.Single so they are precisely equivalent.
Typeof Single and Double Types
MinValue and MaxValue. The MinValue and MaxValue of the float are very large. The values shown in the output are written in standard notation; in scientific notation, these are written as -3.402823E+38 and 3.402823E+38.
Memory usage. The memory usage of floats in the C# language is four bytes per float, which is the same as the int type. If you need to store floating-point numbers in your program and need to save memory, using floats instead of doubles can be beneficial.
There are several constant fields on the float type. This program shows the float.Epsilon constant, which is the smallest float value greater than zero; the NaN constant, which represents not a number; and the NegativeInfinity and PositiveInfinity constants, which can be tested with the float.IsNegativeInfinity and float.IsPositiveInfinity methods.
Program that reveals float constants [C#]
using System;
class Program
{
static void Main()
{
Console.WriteLine(float.Epsilon);
Console.WriteLine(float.NaN);
Console.WriteLine(float.NegativeInfinity);
Console.WriteLine(float.PositiveInfinity);
}
}
Output
1.401298E-45
NaN
-Infinity
Infinity
One major problem with comparing two float values is that the may be almost exactly equal, but not absolutely equal. Your program probably wants to consider the two values equal. For this purpose, you can use the float.Epsilon constant, which is the smallest possible float value. So you can then take the absolute value of the difference between the two values, and see if the difference is less than float.Epsilon. If so, the two values can be considered equal.
Math.Abs
So when should you use the float type in your C# programs? Typically, if you need to use floating point numbers, using the double type instead is a better choice because it has more precision and is probably more common. However, the double type uses more memory than the float type. Therefore, if you are trying to conserve memory, using a float instead of a double can be beneficial if you are sure the loss of precision is immaterial.
Another reason to use float is simply for compatibility (interoperability) with other software that uses floats. It is best to match the types closely to existing software to alleviate inconsistencies between different codebases.

The float type in the C# language is not one of the most commonly needed low-level types. Instead, its best uses are for compatibility issues when you need to represent floating-point numbers. Usually, the double type is better for storing large numbers with decimals. Alternatively, consider the decimal type for the best precision.
Double Decimal Number Examples