Skip to content
Branch: master
Find file Copy path
Find file Copy path
3 contributors

Users who have contributed to this file

@pkulikov @BillWagner @Youssef1313
120 lines (87 sloc) 5.86 KB
title description f1_keywords helpviewer_keywords
Floating-point numeric types - C# reference
Overview of the built-in C# floating-point types
floating-point numbers [C#]
ranges of floating-point types [C#]
size of floating-point types [C#]
types [C#], floating-point types
float keyword [C#]
floating-point numbers [C#], float keyword
double data type [C#]
decimal keyword [C#]

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
float ±1.5 x 10−45 to ±3.4 x 1038 ~6-9 digits 4 bytes xref:System.Single?displayProperty=nameWithType
double ±5.0 × 10−324 to ±1.7 × 10308 ~15-17 digits 8 bytes xref:System.Double?displayProperty=nameWithType
decimal ±1.0 x 10-28 to ±7.9228 x 1028 28-29 digits 16 bytes xref:System.Decimal?displayProperty=nameWithType

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 MinValue and MaxValue constants that provide the minimum and maximum finite value of that type. The float and double types also provide constants that represent not-a-number and infinity values. For example, the double type provides the following constants: xref:System.Double.NaN?displayProperty=nameWithType, xref:System.Double.NegativeInfinity?displayProperty=nameWithType, and xref:System.Double.PositiveInfinity?displayProperty=nameWithType.

Because the decimal type has more precision and a smaller range than both float and 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 double type 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.

You can use either standard numeric format strings or custom numeric format strings to format a floating-point value.

Real literals

The type of a real literal is determined by its suffix as follows:

  • The literal without suffix or with the d or D suffix is of type double
  • The literal with the f or F suffix is of type float
  • The literal with the m or M suffix is of type decimal

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 float to 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:

See also

You can’t perform that action at this time.