浮点数值类型(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# 支持以下预定义浮点类型: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;

每个浮点类型的默认值都为零,0The 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. float and double 类型还提供可表示非数字和无穷大值的常量。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.

可以在表达式中混合使用整型类型和浮点类型。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:

  • 如果其中一个浮点类型是 double,该表达式在关系比较和相等比较中求值类型为 doubleboolIf one of the floating-point types is double, 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.

浮点表达式可以包含下列值集:A floating-point expression can contain the following sets of values:

  • 正和负零Positive and negative zero
  • 正和负无穷大Positive and negative infinity
  • 非数字值 (NaN)Not-a-Number value (NaN)
  • 一组有限的非零值The finite set of nonzero values

有关这些值的详细信息,请参阅 IEEE 网站中提供的二进制浮点算术的 IEEE 标准。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:

  • 不带后缀的文本或带有 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

下面的代码演示每种类型的示例: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 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

转换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