简体   繁体   English

在c#中浮动并加倍

[英]Float and double in c#

Is it necessary to specify f while initiating a float type variable. 是否有必要在启动float类型变量时指定f

float a =3455.67f;

If I declare and initiate it as 如果我宣布并启动它

float a = 3455.67;

Then what will happen? 然后会发生什么?

The documentation on float says: 关于float文档说:

By default, a real numeric literal on the right side of the assignment operator is treated as double . 默认情况下,赋值运算符右侧的实数数字文字被视为double Therefore, to initialize a float variable, use the suffix f or F . 因此,要初始化float变量,请使用后缀fF

This means that if you do float a = 3455.67; 这意味着如果你做float a = 3455.67; then the compiler will refuse to implicitly convert the double to a float . 然后编译器将拒绝隐式地将double转换为float

By default, a real numeric literal on the right side of the assignment operator is treated as double. 默认情况下,赋值运算符右侧的实数数字文字被视为double。 Therefore, to initialize a float variable, use the suffix f or F, as in the following example: 因此,要初始化float变量,请使用后缀f或F,如下例所示:

float x = 3.5F;

If you do not use the suffix in the previous declaration, you will get a compilation error because you are trying to store a double value into a float variable. 如果您不在前一个声明中使用后缀,则会出现编译错误,因为您尝试将double值存储到float变量中。

for more details look at msdn 有关详细信息,请查看msdn

This : 这个 :

float a = 3455.67;

will not compile. 不会编译。 3455.67 is a double constant and C# will permit you to assign this value to a float variable. 3455.67是一个双常量,C#允许您将此值赋给float变量。

Use: 采用:

float f = (float)3455.67;

or you will have to specify the "f" format suffix. 或者你必须指定“f”格式后缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM