# 浮点数值类型（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;


decimalfloat 相比，double 类型具有更高的精度和更小的范围，因此它适合于财务和货币计算。Because the decimal type has more precision and a smaller range than both float and double, it's appropriate for financial and monetary calculations.

• 如果表达式中有 double 类型，则表达式在关系比较和相等比较中求值得到 doubleboolIf there is double type in the expression, the expression evaluates to double, or to bool in relational and equality comparisons.
• 如果表达式中没有 double 类型，则表达式在关系比较和相等比较中求值得到 floatboolIf 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

• 不带后缀的文本或带有 dD 后缀的文本的类型为 doubleThe literal without suffix or with the d or D suffix is of type double
• 带有 fF 后缀的文本的类型为 floatThe literal with the f or F suffix is of type float
• 带有 mM 后缀的文本的类型为 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