#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;

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #23 – Real Literals

  1. kai zhou says:

    Thank you Sean.

    The suffix could also be:

    float — suffix with ‘F’ e.g. float f2 = 0.1F;
    double –suffix with ‘D’ e.g. double d2 = 0.1D;
    decmial –suffix with ‘M’ e.g. decimal dm2 = 0.1M;

    tested on Visual Studio 2013, Version Update 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: