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

## 浮點數型別的特性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

double a = 12.3;
System.Double b = 12.3;


• 如果運算式中有 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.

double a = 1.0;
decimal b = 2.1m;
Console.WriteLine(a + (double)b);
Console.WriteLine((decimal)a + b);


## 實際常值Real literals

• 不含尾碼或具有 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

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;


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