C# double
last modified October 15, 2026
In this article we show how to work with double data type in C#.
The double type is one of the fundamental data types in
programming. We can define a data type as a set of values, and the
allowable operations on those values.
The double is a primitive data type which represents real numbers
in computing. It is a 64-bit binary floating point type that follows the
IEEE 754 standard. The double is an alias for the
System.Double structure.
Floating point numbers represent real numbers in computing. Real numbers measure
continuous quantities, like weight, height, or speed. In C# we have three
floating point types: float, double, and
decimal.
| C# Alias | .NET Type | Size | Precision | Range |
|---|---|---|---|---|
| float | System.Single | 4 bytes | ~6-9 digits | ±1.5 x 10-45 to ±3.4 x 1038 |
| double | System.Double | 8 bytes | ~15-17 digits | ±5.0 x 10-324 to ±1.7 x 10308 |
| decimal | System.Decimal | 16 bytes | 28-29 digits | ±1.0 x 10-28 to ±7.9228 x 1028 |
The above table gives the characteristics of the floating point types.
C# double type constants
The double type has some built-in constants.
Console.WriteLine(double.MinValue); Console.WriteLine(double.MaxValue); Console.WriteLine(double.E); Console.WriteLine(double.Pi); Console.WriteLine(double.Tau);
The program prints five constants.
Console.WriteLine(double.MinValue); Console.WriteLine(double.MaxValue);
We print the smallest and largest possible value of the double type.
Console.WriteLine(double.E); Console.WriteLine(double.Pi); Console.WriteLine(double.Tau);
We print three well-known mathematical constants.
$ dotnet run -1.7976931348623157E+308 1.7976931348623157E+308 2.718281828459045 3.141592653589793 6.283185307179586
Floating point type suffixes
By default, real number literals are of the double type in C#
programs. To use a different type, we must use a suffix: F/f for
float numbers and M/m for decimal
numbers.
float n1 = 6.78f; double n2 = 6.78; decimal n3 = 6.78m; Console.WriteLine(n1); Console.WriteLine(n2); Console.WriteLine(n3); Console.WriteLine(n1.GetType()); Console.WriteLine(n2.GetType()); Console.WriteLine(n3.GetType());
In the above program, we use three different literal notations for floating point numbers.
float n1 = 6.78f;
The f suffix is used for a float number.
double n2 = 6.78;
If we do not use a suffix, then it is a double number. We
can optionally use the d suffix.
Console.WriteLine(n1.GetType());
The GetType method returns the type of the number.
$ dotnet run 6.78 6.78 6.78 System.Single System.Double System.Decimal
C# convert to double
In the next example, we show how to convert values to double type.
float n1 = 6.78f;
decimal n2 = 6.78m;
int n3 = 7;
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.WriteLine(n3);
Console.WriteLine("-------------------");
double d1 = (double) n1;
double d2 = decimal.ToDouble(n2);
double d3 = Convert.ToDouble(n3);
Console.WriteLine(d1);
Console.WriteLine(d2);
Console.WriteLine(d3);
Console.WriteLine("-------------------");
Console.WriteLine(d1.GetType());
Console.WriteLine(d2.GetType());
Console.WriteLine(d3.GetType());
We define three variables having float, decimal, and
int types. We convert these values to double type.
float n1 = 6.78f; decimal n2 = 6.78m; int n3 = 7;
We define three variables.
double d1 = (double) n1;
We use the casting operation to convert the float value to
double.
double d2 = decimal.ToDouble(n2);
In the second case, we convert the decimal to double
with decimal.ToDouble built-in method.
double d3 = Convert.ToDouble(n3);
Finally, for the integer type, we utilize the Convert class.
An int can also be converted implicitly, without any cast.
$ dotnet run 6.78 6.78 7 ------------------- 6.78000020980835 6.78 7 ------------------- System.Double System.Double System.Double
Note that there is some small error in the first conversion. The value 6.78
cannot be represented exactly in float; the conversion to
double reveals the approximation.
C# convert from double type
In the next example we show how to convert from the double type
to other data types.
double val = 6.78; float n1 = (float) val; decimal n2 = Convert.ToDecimal(val); int n3 = Convert.ToInt32(val); Console.WriteLine(n1); Console.WriteLine(n2); Console.WriteLine(n3); Console.WriteLine(n1.GetType()); Console.WriteLine(n2.GetType()); Console.WriteLine(n3.GetType());
We define a single double value. We convert the value to float,
decimal, and int. Note that
Convert.ToInt32 rounds the value to the nearest integer, while an
(int) cast would truncate it to 6.
$ dotnet run 6.78 6.78 7 System.Single System.Decimal System.Int32
The double type is inexact
The double type is not exact. There are small rounding errors in
calculations. In many cases, these do not pose a problem. For instance, it does
not matter if an item is 1.78 mm or 1.7801 mm long.
In financial and currency calculations, these small errors matter. For those
types of calculations, we use the decimal type.
double n1 = 0.1 + 0.1 + 0.1;
double n2 = 0.3;
Console.WriteLine(n1);
Console.WriteLine(n2);
if (n1 == n2)
{
Console.WriteLine("The values are equal");
} else
{
Console.WriteLine("The values are not equal");
}
The program demonstrates a small error in a simple calculation with
double types.
$ dotnet run 0.30000000000000004 0.3 The values are not equal
The two values are not equal because there is a very small error in the addition operation.
Therefore, double values should not be compared with the
== operator. Instead, we check if the difference of the values is
smaller than some tolerance.
double n1 = 0.1 + 0.2; double n2 = 0.3; const double tolerance = 1e-9; Console.WriteLine(n1 == n2); Console.WriteLine(Math.Abs(n1 - n2) < tolerance);
The values are considered equal if their difference is smaller than the tolerance.
$ dotnet run False True
C# double special values
Floating point division by zero does not throw an exception. The result is positive or negative infinity, or NaN (not a number).
double x = 1.0 / 0; double y = -1.0 / 0; double z = 0.0 / 0; Console.WriteLine(x); Console.WriteLine(y); Console.WriteLine(z); Console.WriteLine(double.IsInfinity(x)); Console.WriteLine(double.IsNaN(z)); Console.WriteLine(z == double.NaN);
We create three special values and check them with the
double.IsInfinity and double.IsNaN methods.
Console.WriteLine(z == double.NaN);
NaN is not equal to any value, not even to itself. Therefore, we must use the
double.IsNaN method to check for it.
$ dotnet run ∞ -∞ NaN True True False
C# parse double
The double.TryParse method converts a string to a
double. It returns a boolean value indicating whether the
conversion succeeded.
using System.Globalization;
string[] vals = ["3.14", "2.5e3", "-0.001", "abc"];
foreach (var val in vals)
{
if (double.TryParse(val, CultureInfo.InvariantCulture, out double res))
{
Console.WriteLine($"{val} -> {res}");
}
else
{
Console.WriteLine($"{val} -> cannot parse");
}
}
We try to parse four strings. We use the invariant culture, in which the dot is the decimal separator.
$ dotnet run 3.14 -> 3.14 2.5e3 -> 2500 -0.001 -> -0.001 abc -> cannot parse
C# round double values
Rounding numbers is adjusting the digits up or down to make calculations easier.
We can round double values with the double.Round or
Math.Round methods.
double double.Round(double x, int digits, MidpointRounding mode)
The method rounds a double value to a specified number of fractional digits using the specified rounding mode.
| Mode | Description |
|---|---|
| AwayFromZero | a midpoint value is rounded toward the nearest number that's away from zero |
| ToEven | a midpoint value is rounded toward the nearest even number (default) |
| ToNegativeInfinity | downwards-directed rounding |
| ToPositiveInfinity | upwards-directed rounding |
| ToZero | rounding towards zero |
The table presents the rounding modes available.
double n1 = 1 / 7d;
double n2 = -1 / 7d;
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.WriteLine("-------------------------");
Console.WriteLine(double.Round(n1, 3, MidpointRounding.ToEven));
Console.WriteLine(double.Round(n2, 3, MidpointRounding.ToEven));
Console.WriteLine("-------------------------");
Console.WriteLine(double.Round(n1, 3, MidpointRounding.AwayFromZero));
Console.WriteLine(double.Round(n2, 3, MidpointRounding.AwayFromZero));
Console.WriteLine("-------------------------");
Console.WriteLine(double.Round(n1, 3, MidpointRounding.ToZero));
Console.WriteLine(double.Round(n2, 3, MidpointRounding.ToZero));
Console.WriteLine("-------------------------");
Console.WriteLine(double.Round(n1, 3, MidpointRounding.ToNegativeInfinity));
Console.WriteLine(double.Round(n2, 3, MidpointRounding.ToNegativeInfinity));
Console.WriteLine("-------------------------");
Console.WriteLine(double.Round(n1, 3, MidpointRounding.ToPositiveInfinity));
Console.WriteLine(double.Round(n2, 3, MidpointRounding.ToPositiveInfinity));
In the example, we have a positive and a negative double value. We round the values using the available rounding modes to three fractional digits.
$ dotnet run 0.14285714285714285 -0.14285714285714285 ------------------------- 0.143 -0.143 ------------------------- 0.143 -0.143 ------------------------- 0.142 -0.142 ------------------------- 0.142 -0.143 ------------------------- 0.143 -0.142
Source
In this article we worked with the double type in C#.
Author
List all C# tutorials.