#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 .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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

Follow

Get every new post delivered to your Inbox.

Join 43 other followers