Float and double in c #

Do I need to specify f when running a variable of type float.

 float a =3455.67f; 

If I declare and run it as

 float a = 3455.67; 

Then what will happen?

+4
source share
3 answers

documentation on float says:

By default, the actual numeric literal on the right side of the job is treated as a double operator. Therefore, to initialize a float variable, use the suffix f or f .

This means that if you do float a = 3455.67; , then the compiler will refuse the implicit conversion of double to float .

+4
source

By default, the real numeric literal on the right side of the assignment operator is treated as double. Therefore, to initialize the variable float, use the suffix f or F, as in the following example:

 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 in the float variable.

see msdn for more details

+2
source

It:

 float a = 3455.67; 

will not compile. 3455.67 is a double constant, and C # will allow you to assign this value to the float variable.

Using:

 float f = (float)3455.67; 

or you will need to specify a suffix of the format "f".

+2
source

Source: https://habr.com/ru/post/1397016/


All Articles