Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float and double in c#

Tags:

c#

Is it necessary to specify f while initiating a float type variable.

float a =3455.67f;

If I declare and initiate it as

float a = 3455.67;

Then what will happen?

like image 324
Eljay Avatar asked Feb 17 '12 11:02

Eljay


2 Answers

The documentation on float says:

By default, a real numeric literal on the right side of the assignment operator is treated as double. 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 to implicitly convert the double to a float.

like image 101
Jon Avatar answered Nov 11 '22 09:11

Jon


By default, a real numeric literal on the right side of the assignment operator is treated as double. Therefore, to initialize a float variable, 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 into a float variable.

for more details look at msdn

like image 22
Serghei Avatar answered Nov 11 '22 10:11

Serghei