为什么"餐饮QUOT;花车声明时,需要? [英] Why is the "f" required when declaring floats?
问题描述
例如:
float timeRemaining = 0.58f;
为什么˚F
是在数端所需?
推荐答案
您一个浮动的声明包含两个部分:
Your declaration of a float contains two parts:
- 这声明变量
timeRemaining
的类型为浮动
。 - 它将值
0.58
这个变量。
- It declares that the variable
timeRemaining
is of typefloat
. - It assigns the value
0.58
to this variable.
在第2部分出现问题。
右手侧自行评估。根据C#规范,内容不具有后缀小数点的数字是PTED为跨$ P $一个双击
。
The right-hand side is evaluated on its own. According to the C# specification, a number containing a decimal point that doesn't have a suffix is interpreted as a double
.
所以,我们现在有一个,我们想分配给键入
值。为了做到这一点,必须有从浮动
的变量双的隐式转换的双
到浮动
。有没有这样的转换,因为你可能(在这种情况下做)在转换丢失信息。
So we now have a double
value that we want to assign to a variable of type float
. In order to do this, there must be an implicit conversion from double
to float
. There is no such conversion, because you may (and in this case do) lose information in the conversion.
其原因是由编译器使用的值是不是真的0.58,但最接近0.58浮点值,这是0.57999999999999978655962351581366 ...为双击
,准确0.579999946057796478271484375为浮动
。
The reason is that the value used by the compiler isn't really 0.58, but the floating-point value closest to 0.58, which is 0.57999999999999978655962351581366... for double
and exactly 0.579999946057796478271484375 for float
.
严格说来,˚F
不是必需的。您可避免由值转换为一浮动
使用˚F
说明:
Strictly speaking, the f
is not required. You can avoid having to use the f
suffix by casting the value to a float
:
float timeRemaining = (float)0.58;
这篇关于为什么"餐饮QUOT;花车声明时,需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!