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

每个浮点类型的默认值都为零,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.

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.

可在表达式中将整型类型与 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 类型,则表达式在关系比较和相等比较中求值得到 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.

你还可在表达式中混合使用整型类型和 decimal 类型。You can also mix integral types and the decimal type in an expression. 在这种情况下,整型类型隐式转换为 decimal 类型,并且表达式在关系比较和相等比较中求值得到 decimalboolIn 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:

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