Numerische Gleitkommatypen (C#-Referenz)Floating-point numeric types (C# reference)
Die numerischen Gleitkommatypen stellen reelle Zahlen dar.The floating-point numeric types represent real numbers. Alle numerischen Gleitkommatypen sind Werttypen.All floating-point numeric types are value types. Sie sind auch einfache Typen und können mit Literalen initialisiert werden.They are also simple types and can be initialized with literals. Alle numerischen Gleitkommatypen unterstützen arithmetic-, comparison- und equality-Operatoren.All floating-point numeric types support arithmetic, comparison, and equality operators.
Merkmale der GleitkommatypenCharacteristics of the floating-point types
C# unterstützt die folgenden vordefinierten Gleitkommatypen:C# supports the following predefined floating-point types:
C#-Typ/SchlüsselwortC# type/keyword | Ungefährer BereichApproximate range | GenauigkeitPrecision | GrößeSize | .NET-Typ.NET type |
---|---|---|---|---|
float |
±1.5 × 10−45 zu ±3.4 × 1038±1.5 x 10−45 to ±3.4 x 1038 | ~6–9 Stellen~6-9 digits | 4 Bytes4 bytes | System.Single |
double |
±5,0 × 10−324 bis ±1,7 × 10308±5.0 × 10−324 to ±1.7 × 10308 | ~15–17 Stellen~15-17 digits | 8 Bytes8 bytes | System.Double |
decimal |
±1.0 × 10-28 to ±7.9228 × 1028±1.0 x 10-28 to ±7.9228 x 1028 | 28-29 Stellen28-29 digits | 16 Bytes16 bytes | System.Decimal |
In der obigen Tabelle ist jedes C#-Typschlüsselwort aus der äußerst linken Spalte ein Alias für den entsprechenden .NET-Typ.In the preceding table, each C# type keyword from the leftmost column is an alias for the corresponding .NET type. Sie können synonym verwendet werden.They are interchangeable. In den folgenden Deklarationen werden beispielsweise Variablen des gleichen Typs deklariert:For example, the following declarations declare variables of the same type:
double a = 12.3;
System.Double b = 12.3;
Der Standardwert jedes Gleitkommatyps ist Null (0
).The default value of each floating-point type is zero, 0
. Die einzelnen Gleitkommatypen verfügen jeweils über die Konstanten MinValue
und MaxValue
, die den minimalen und maximalen Endwert des Typs angeben.Each of the floating-point types has the MinValue
and MaxValue
constants that provide the minimum and maximum finite value of that type. Die Typen float
und double
verfügen auch über Konstanten, die nicht numerische Werte und Unendlichkeitswerte darstellen.The float
and double
types also provide constants that represent not-a-number and infinity values. Der Typ double
verfügt beispielsweise über folgende Konstanten: Double.NaN, Double.NegativeInfinity und Double.PositiveInfinity.For example, the double
type provides the following constants: Double.NaN, Double.NegativeInfinity, and Double.PositiveInfinity.
Da der Typ decimal
über eine höhere Genauigkeit und einen kleineren Bereich verfügt als float
und double
, eignet er sich für Finanz- und Währungskalkulationen.Because the decimal
type has more precision and a smaller range than both float
and double
, it's appropriate for financial and monetary calculations.
Sie können integrale Typen sowie die Typen float
und double
in einem Ausdruck kombinieren.You can mix integral types and the float
and double
types in an expression. In diesem Fall werden integrale Typen implizit in einen der Gleitkommatypen konvertiert. Bei Bedarf wird der float
-Typ implizit in double
konvertiert.In this case, integral types are implicitly converted to one of the floating-point types and, if necessary, the float
type is implicitly converted to double
. Der Ausdruck wird wie folgt ausgewertet:The expression is evaluated as follows:
- Wenn der
double
-Typ im Ausdruck vorhanden ist, wird der Ausdruck indouble
oder in relationalen Vergleichen oder Vergleichen auf Gleichheit inbool
ausgewertet.If there isdouble
type in the expression, the expression evaluates todouble
, or tobool
in relational and equality comparisons. - Wenn der
double
-Typ im Ausdruck vorhanden ist, wird der Ausdruck infloat
oder in relationalen Vergleichen oder Vergleichen auf Gleichheit inbool
ausgewertet.If there is nodouble
type in the expression, the expression evaluates tofloat
, or tobool
in relational and equality comparisons.
Sie können integrale Typen und den decimal
-Typ auch in einem Ausdruck miteinander kombinieren.You can also mix integral types and the decimal
type in an expression. In diesem Fall werden integrale Typen implizit in den decimal
-Typ konvertiert, und der Ausdruck wird als decimal
oder bool
in relationalen Vergleichen und Gleichheitsvergleichen ausgewertet.In this case, integral types are implicitly converted to the decimal
type and the expression evaluates to decimal
, or to bool
in relational and equality comparisons.
In einem Ausdruck können Sie den decimal
-Typ nicht mit den Typen float
und double
kombinieren.You cannot mix the decimal
type with the float
and double
types in an expression. Wenn Sie aber einen arithmetischen Vorgang, einen Vergleich oder einen Gleichheitsvorgang durchführen möchten, müssen Sie, wie nachfolgend dargestellt, in diesem Fall die Operanden explizit aus dem oder in den decimal
-Typ konvertierenIn this case, if you want to perform arithmetic, comparison, or equality operations, you must explicitly convert the operands either from or to the decimal
type, as the following example shows:
double a = 1.0;
decimal b = 2.1m;
Console.WriteLine(a + (double)b);
Console.WriteLine((decimal)a + b);
Zum Formatieren eines Gleitkommawerts können Sie standardmäßige Zahlenformatzeichenfolgen oder benutzerdefinierte Zahlenformatzeichenfolgen verwenden.You can use either standard numeric format strings or custom numeric format strings to format a floating-point value.
Real-LiteraleReal literals
Der Typ eines Real-Literals wird wie folgt durch sein Suffix bestimmt:The type of a real literal is determined by its suffix as follows:
- Das Literal ohne Suffix oder mit dem Suffix
d
oderD
ist vom Typdouble
.The literal without suffix or with thed
orD
suffix is of typedouble
- Das Literal mit dem Suffix
f
oderF
ist vom Typfloat
.The literal with thef
orF
suffix is of typefloat
- Das Literal mit dem Suffix
m
oderM
ist vom Typdecimal
.The literal with them
orM
suffix is of typedecimal
Der folgende Code zeigt ein Beispiel für jeden Typ: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;
Das vorherige Beispiel zeigt auch die Verwendung von _
als Zifferntrennzeichen, das ab C# 7.0 unterstützt wird.The preceding example also shows the use of _
as a digit separator, which is supported starting with C# 7.0. Sie können das Zifferntrennzeichen mit allen Arten numerischer Literale verwenden.You can use the digit separator with all kinds of numeric literals.
Sie können auch die wissenschaftliche Notation verwenden, d. h. einen exponentiellen Teil eines Real-Literals angeben, wie das folgende Beispiel zeigt:You can also 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
KonvertierungenConversions
Es gibt nur eine implizite Konvertierung zwischen numerischen Gleitkommatypen: von float
zu double
.There is only one implicit conversion between floating-point numeric types: from float
to double
. Allerdings können Sie einen Gleitkommatyp mit der expliziten Umwandlungin beliebige andere Gleitkommatypen konvertieren.However, you can convert any floating-point type to any other floating-point type with the explicit cast. Weitere Informationen finden Sie unter Integrierte numerische Konvertierungen (C#-Referenz) (Integrierte numerische Konvertierungen).For more information, see Built-in numeric conversions.
C#-SprachspezifikationC# language specification
Weitere Informationen finden Sie in den folgenden Abschnitten der C#-Sprachspezifikation:For more information, see the following sections of the C# language specification: