Since, we would have already used datatypes "float" and "double" datatypes while defining the datatypes… Why is it necessary to add "F" or "D" at the end? Would
float a = 12.3;
double b = 12.333334656;
Why doesn’t the above declaration work? But, the below code works just fine.
float a = 12.3f;
double b = 12.333334656;
2
Answers
It is not "compulsory", as stated in the question title. However, you need to be unambiguous, otherwise you would have problems when casting.
The list below has all the explicit number types:
You can read more about real literals here.
The issue is that expression 12.3.
So, when the expression can be of "many" types, then you have to cast the type, or at the very least tell the computer (compiler) what type that expression is to be.
So, say this would work:
So, in most cases, the compiler will assume that such "real" expressions are in fact single, or double.
However, in the case of using a float, then you have to be explicit in the type as above shows.
So, often we don’t notice, don’t care, and don’t worry since .net will set/guess the correct data type for you.
However, in the case of floating vs single/double numbers? Then you have force the issue and tell .net that the expression in question is to be floating as opposed to the default assumption of single or double.