Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Decimal literal and Float literal

Posted on: 2011-10-06

Some may don't know but if you want to write a double value in code you cannot write:

 double myVariable = 100; 

This won't work because the value will be treated as an integer. To resolve this issue, the simplest way to do it is to mark it as decimal to the compiler with the suffix 'd' or 'D'.

 double myVariable = 100d; //or double myVariable = 100D; 

The same thing is good for float type. Instead of using the literal 'd' or 'D' you need to use the character 'F' or 'f'.

This is so basic that sometime you may wonder why Visual Studio mark it as an error. You could ask yourself also what is the character to mark a number into a specific type like double or float. Well, the first letter of each of type is the answer.