The float keyword denotes a simple type that stores 32-bit floating-point values. The following table shows the precision and approximate range for the float type.
Type | Approximate range | Precision | NGWS Type |
---|---|---|---|
float | ±1.5 × 10-45 to ±3.4 × 1038 | 7 digits | System.Single |
By default, a real numeric literal on the right-hand side of the assignment operator is treated as double. Therefore, to initialize a float variable use the suffix f
or F
, for example:
float x = 3.5F;
If you don't use the suffix in the previous declaration, you will get a compilation error because you are attempting to store a double value into a float variable.
You can mix numeric integral types and floating-point types in an expression. In this case, the integral types are converted to floating-point types. The evaluation of the expression is performed according to the following rules:
A floating-point expression can contain the following sets of values:
For more information on these values, refer to IEEE Standard for Binary Floating-Point Arithmetic, available on the Web site http://www.ieee.org/.
For more information on floating-point value sets, see §4.1.5 in the language reference.
In the following example, an int, a short, and a float are included in a mathematical expression giving a float result (notice that there is no double in the expression).
// Mixing types in expressions using System; class MixedTypes { public static void Main() { int x = 3; float y = 4.5f; short z = 5; Console.WriteLine("The result is {0}", x*y/z); } }
The result is 2.70000005
C# Keywords | Default Values Table | Built-in Types Table | Floating-point Types Table | Implicit Numeric Conversions Table | Explicit Numeric Conversions Table