I was under the impression that it is legal and conventional to declare and initialize a floating point number in this format:
float someVariable = 12.502D; (or M, F does not give a compiler error).
However I get a compiler error:
Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type.
There are three types of floating point numbers in C#, right?
- F or f for float. (7 significant digits)
- D or d for Double. (15 or 16 significant digits)
- M or m for Decimal. (28 or 29 significant digits)
To fix the compiler error I explicitly casted the assignment statement:
float SomeVariable = (float) 12.525D;
Did I do the right thing in this case? What is the conventional or correct way to do it declare and initialize a floating point variable that consists of a Double or a Decimal value?
F
as the compiler suggested? Then no cast is needed (implicitly nor explicitly)… – mousiodouble
variable, declare it as suchdouble SomeVariable = 12.525D;
It just doesn't make sense to declare it as one type and use a literal of a different type. – Damien_The_Unbeliever