例子:
float timeRemaining = 0.58f;
为什么这个数字的末尾需要f?
Why is the f is required at the end of this number?
你的浮点声明包含两部分:
Your declaration of a float contains two parts:
timeRemaining的类型是float.0.58 分配给该变量.timeRemaining is of type float.0.58 to this variable.问题出现在第 2 部分.
The problem occurs in part 2.
右侧是自行评估的.根据 C# 规范,包含没有后缀的小数点的数字被解释为 double.
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.
所以我们现在有一个 double 值,我们希望将其分配给 float 类型的变量.为了做到这一点,必须有一个从 double 到 float 的隐式转换.没有这样的转换,因为您可能(并且在这种情况下确实)在转换中丢失了信息.
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 的浮点值,即 double 为 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 不是必需的.您可以通过将值转换为 float 来避免使用 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;
这篇关于为什么是“f"?声明浮动时需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
ASP.NET Core 使用 Azure Active Directory 进行身份验证并ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 进行身
ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用)
如何获取守护进程或服务器到 C# ASP.NET Web API 的How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.N
异步调用时 Azure KeyVault Active Directory AcquireTokenAAzure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(异步调用时 Azure KeyVault Active Directory AcquireTokenAsync 超
使用电子邮件地址和应用程序密码从 oauth2/tokenGetting access token using email address and app password from oauth2/token(使用电子邮件地址和应用程序密码从 oauth2/token 获取访问令
新的 Azure AD 应用程序在通过管理门户更新之前无New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 应用程序在通过管理门户更新之前无法运行