C# float (Floating-point)

The float data type is used to store floating-point numbers but with less precision compared to double. It is typically used when memory savings are more important than precision.

Key Concepts

  • The float type is a 32-bit single-precision floating-point number.
  • It is sufficient for most basic calculations where high precision is not required.
  • Always append an 'F' to the value to explicitly mark it as a float.

Example of Using a float

Code Example

// Declare a float variable
float myFloat = 5.75F;

// Output the float to the console
Console.WriteLine(myFloat);

Output:

5.75

Code Explanation: The float variable myFloat is initialized with the value 5.75. The 'F' denotes that the number is a float. It is printed using the Console.WriteLine() method.

Output Explanation: The value 5.75 is displayed on the console, which is the value assigned to myFloat.