Lesson 1.3.3 Decimal

Extra accurate and holds decimal values, what more could you want? Well, possibly a little less memory usage. If accuracy of decimal points is not that important to you then you might want to consider float.

  decimal myVariable = 3.141592m;

Note that above, I put the letter “m” after the number. This is so C# knows what type of value the number is. The letter stands for “money”. No I’m not sure why they call it a decimal and then denote it as money either.

Compare this to the float below. I have post-fixed the value with “f” to show C# that it is a floating point number.

  float myFloat = 3.141592f;

The issue stems from the fact that raw decimal numbers you place into the code default to being double values. When creating a double, as in the example below, you need no suffix to denote its type.

  double myDouble = 3.141592;

However you could have put a “d” after the number to denote it as a double. C# doesn’t really mind.

This makes it extra fun when handling non-whole numbers. But it makes sense when you take into consideration the smorgasbord of options available and the types of application they can have.

For instance, if you’re making incredibly accurate scientific calculations then decimal is the way to go. If you’re slapping up values to work out someone’s shopping list then you can save yourself some memory and use a float. They’re hardly going to care about it being more than 2 decimal places