#23 – Real Literals

There are several ways to indicate real number (floating point) literals in C#.  Real literals are assumed to be of type double, but may also be of type float or decimal, depending on the suffix included with the literal.

Suffix / type:

  • f – float
  • d – double
  • m – decimal

Here are some examples:

 // Suffixes
 double d1 = 1.0;    // ok
 object o1 = 1.0;    // Also double
 float f1 = 3f;      // float
 float f2 = 1.0f;    // Must have 'f' suffix for float
 double d2 = 1.0d;   // Optional 'd' suffix
 decimal d3 = 1.0m;

 // Exponents
 double d4 = 1.2E3;    // 1200
 double d5 = 1.2E+3;   // 1200
 double d6 = 1.2E-3;   // 0.0012
 double d7 = 2E3;      // 2000
 double d8 = 2E-3;     // 0.002
 float f3 = 2E-3f;
 decimal d9 = 2E-3m;
Advertisements