Floating-point numeric types (C# reference)
The floating-point types are a subset of the simple types and can be initialized with literals. All floating-point types are also value types. All floating-point numeric types support arithmetic, comparison, and equality operators.
Characteristics of the floating-point types
C# supports the following predefined floating-point types:
|C# type/keyword||Approximate range||Precision||Size||.NET type|
||±1.5 x 10−45 to ±3.4 x 1038||~6-9 digits||4 bytes||System.Single|
||±5.0 × 10−324 to ±1.7 × 10308||~15-17 digits||8 bytes||System.Double|
||±1.0 x 10-28 to ±7.9228 x 1028||28-29 digits||16 bytes||System.Decimal|
In the preceding table, each C# type keyword from the leftmost column is an alias for the corresponding .NET type. They are interchangeable. For example, the following declarations declare variables of the same type:
double a = 12.3; System.Double b = 12.3;
The default value of each floating-point type is zero,
0. Each of the floating-point types has the
MaxValue constants that provide the minimum and maximum finite value of that type. The
double types also provide constants that represent not-a-number and infinity values. For example, the
double type provides the following constants: Double.NaN, Double.NegativeInfinity, and Double.PositiveInfinity.
decimal type has more precision and a smaller range than both
double, it's appropriate for financial and monetary calculations.
You can mix 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:
- If one of the floating-point types is
double, the expression evaluates to
double, or to bool in relational and equality comparisons.
- If there is no
doubletype in the expression, the expression evaluates to
float, or to bool in relational and equality comparisons.
A floating-point expression can contain the following sets of values:
- Positive and negative zero
- Positive and negative infinity
- Not-a-Number value (NaN)
- The finite set of nonzero values
For more information about these values, see IEEE Standard for Binary Floating-Point Arithmetic, available on the IEEE website.
The type of a real literal is determined by its suffix as follows:
- The literal without suffix or with the
Dsuffix is of type
- The literal with the
Fsuffix is of type
- The literal with the
Msuffix is of type
The following code demonstrates an example of each:
double d = 3D; d = 4d; d = 3.934_001; float f = 3_000.5F; f = 5.4f; decimal myMoney = 3_000.5m; myMoney = 400.75M;
The preceding example also shows the use of
_ as a digit separator, which is supported starting with C# 7.0. You can use the digit separator with all kinds of numeric literals.
You also can use scientific notation, that is, specify an exponent part of a real literal, as the following example shows:
double d = 0.42e2; Console.WriteLine(d); // output 42; float f = 134.45E-2f; Console.WriteLine(f); // output: 1.3445 decimal m = 1.5E6m; Console.WriteLine(m); // output: 1500000
There is only one implicit conversion between floating-point numeric types: from
double. However, you can convert any floating-point type to any other floating-point type with the explicit cast. For more information, see Built-in numeric conversions.
C# language specification
For more information, see the following sections of the C# language specification: