浮點數值型別 (C# 參考)Floating-point numeric types (C# reference)

浮點數數值型別代表實數。The floating-point numeric types represent real numbers. 所有浮點數數值型別都是實數值型別All floating-point numeric types are value types. 它們也是簡單類型,而且可以使用值進行初始化。They are also simple types and can be initialized with literals. 所有浮點數數值型別都支援算術、比較和等號比較運算子。All floating-point numeric types support arithmetic, comparison, and equality operators.

浮點數型別的特性Characteristics of the floating-point types

C# 支援下列預先定義的浮點數型別:C# supports the following predefined floating-point types:

C# 型別/關鍵字C# type/keyword 大概範圍Approximate range 準確率Precision 大小Size .NET 類型.NET type
float ±1.5 x 10−45 到 ±3.4 x 1038±1.5 x 10−45 to ±3.4 x 1038 ~6-9 位數~6-9 digits 4 個位元組4 bytes System.Single
double ±5.0 × 10−324 至 ±1.7 × 10308±5.0 × 10−324 to ±1.7 × 10308 ~15-17 位數~15-17 digits 8 個位元組8 bytes System.Double
decimal ±1.0 x 10-28 到 ±7.9228 x 1028±1.0 x 10-28 to ±7.9228 x 1028 28-29 位數28-29 digits 16 個位元組16 bytes System.Decimal

在上表中,最左邊欄中的每個 C# 型別關鍵字,都是相對應 .NET 型別的別名。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;

每個浮點數型別的預設值都是零 (0)。The default value of each floating-point type is zero, 0. 每個浮點數型別都有 MinValueMaxValue 常數,提供該型別的最小與最大有限值。Each of the floating-point types has the MinValue and MaxValue constants that provide the minimum and maximum finite value of that type. floatdouble 型別也提供表示不是數字和無限值的常數。The float and double types also provide constants that represent not-a-number and infinity values. 例如,double 型別提供下列常數:Double.NaNDouble.NegativeInfinityDouble.PositiveInfinityFor example, the double type provides the following constants: Double.NaN, Double.NegativeInfinity, and Double.PositiveInfinity.

相較於 floatdouble,因為 decimal 型別的精確度較高且範圍較小,所以非常適合財務與金融計算。Because the decimal type has more precision and a smaller range than both float and double, it's appropriate for financial and monetary calculations.

您可以在運算式中混合整數類資料類型和 floatdouble 類型。You can mix integral types and the float and double types in an expression. 在此情況下,整數類資料類型會隱含地轉換成其中一個浮點類型,並在必要時,將 float 類型隱含地轉換成 doubleIn 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. 運算式評估如下:The expression is evaluated as follows:

  • 如果運算式中有 double 類型,則運算式會評估為 double ,或 bool 在關聯式和相等比較中為。If there is double type in the expression, the expression evaluates to double, or to bool in relational and equality comparisons.
  • 如果運算式中沒有 double 類型,則運算式會評估為 float ,或 bool 在關聯式和相等比較中為。If there is no double type in the expression, the expression evaluates to float, or to bool in relational and equality comparisons.

您也可以在運算式中混合整數類資料類型和 decimal 類型。You can also mix integral types and the decimal type in an expression. 在此情況下,整數類資料類型會隱含地轉換成 decimal 類型,而運算式會 decimal bool 在關聯式和相等比較中,評估為,或為。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.

您不能 decimal 在運算式中混用類型與 floatdouble 類型。You cannot mix the decimal type with the float and double types in an expression. 在此情況下,如果您想要執行算術、比較或相等運算,您必須明確地將運算元從或轉換成 decimal 類型,如下列範例所示:In 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);

您可以使用標準數值格式字串自訂數值格式字串,設定浮點數值格式。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:

  • 不含尾碼或具有 d 或尾碼的常 D 值屬於類型doubleThe literal without suffix or with the d or D suffix is of type double
  • 具有 f 或尾碼的常 F 值屬於類型floatThe literal with the f or F suffix is of type float
  • 具有 m 或尾碼的常 M 值屬於類型decimalThe 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;

上述範例也會示範如何使用 _ 做為數位分隔符號,從 c # 7.0 開始支援。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 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

轉換Conversions

浮點數數值型別之間只有一個隱含轉換:從 floatdoubleThere 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# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格的下列幾節:For more information, see the following sections of the C# language specification:

另請參閱See also