浮点数值类型(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 .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 System.Single
double ±5.0 × 10−324 到 ±1.7 × 10308±5.0 × 10−324 to ±1.7 × 10308 大约 15-17 位数字~15-17 digits 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 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 comparisons or comparisons for equality.
  • 如果表达式中没有 double 类型,则表达式在关系比较或相等比较中求值类型为 floatboolIf there is no double type in the expression, the expression evaluates to float, or to bool in relational comparisons or comparisons for equality.

浮点表达式可以包含下列值集: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.

浮点文本Floating-point literals

默认情况下,赋值运算符右侧的浮点数值文本被视为 doubleBy default, a floating-point numeric literal on the right side of the assignment operator is treated as double. 后缀可用于将浮点或整型文本转换为特定类型:You can use suffixes to convert a floating-point or integral literal to a specific type:

  • dD 后缀用于将文本转换为 doubleThe d or D suffix converts a literal to a double.
  • fF 后缀用于将文本转换为 floatThe f or F suffix converts a literal to a float.
  • mM 后缀用于将文本转换为 decimalThe m or M suffix converts a literal to a decimal.

下面的示例显示了每种后缀:The following examples show each suffix:

double d = 3D;
d = 4d;
float f = 3.5F;
f = 5.4f;
decimal myMoney = 300.5m;
myMoney = 400.75M;

转换Conversions

存在从 floatdouble 的隐式转换(称为扩大转换),因为 float 值的范围是 double 的正确子集,并且从 floatdouble 不会丢失精度 。There's an implicit conversion (called a widening conversion) from float to double because the range of float values is a proper subset of double and there is no loss of precision from float to double.

未定义从源类型到目标类型的隐式转换时,必须使用显式强制转换将一个浮点类型转换为另一个浮点类型。You must use an explicit cast to convert one floating-point type to another floating-point type when an implicit conversion isn't defined from the source type to the destination type. 这称为收缩转换 。This is called a narrowing conversion. 由于转换可能导致数据丢失,因此必须使用显式用例。The explicit case is required because the conversion can result in data loss. 其他浮点类型与 decimal 类型之间不存在隐式转换,因为 decimal 类型的精度高于 floatdoubleThere's no implicit conversion between other floating-point types and the decimal type because the decimal type has greater precision than either float or double.

有关隐式数值转换的详细信息,请参阅隐式数值转换表For more information about implicit numeric conversions, see Implicit Numeric Conversions Table.

有关显式数值转换的详细信息,请参阅显式数值转换表For more information about explicit numeric conversions, see Explicit Numeric Conversions Table.

请参阅See also