130

If I want to use a decimal literal in code, I have seen that there exists the m-suffix (where m stands for money). Is this appropriate for any decimals or is there a more general assignment (d stands for double, that is for sure not the right thing although a direct conversion is supported).

object decimalValue=2m;

Please note, I took the object-assignment as example, because in the case of ...

decimal decimalValue=2;

... it's implicitly clear that 2 should be interpreted as decimal through the compiler.

m seems to be ok, msdn uses it as example for the decimal type.

Pang
  • 9,564
  • 146
  • 81
  • 122
HCL
  • 36,053
  • 27
  • 163
  • 213
  • 1
    M could stand for money but it could just be the next available letter of the word “decimal” because D is used for double and E is used as the exponential symbol as in ‘1E06’ for example. – Caltor Mar 30 '21 at 22:46

3 Answers3

276

Documented in the C# language specification, chapter 2.4.4:

float f = 1.2f;
double d = 1.2d;
uint u = 2u;
long l = 2L;
ulong ul = 2UL;
decimal m = 2m;

Nothing for int, byte, sbyte, short, ushort.

x-Dave-x
  • 13
  • 5
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Whilst not a suffix, we can also declare a character with `char c = 'a'` using apostrophes around the character. – Kevin Hogg Aug 26 '15 at 08:05
  • You can also refer to this Doc, which is more user-friendly: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types-table – Douglas Gaskell Jul 29 '19 at 18:07
23

Without a suffix, a numerical real literal will be a Double. The m suffix specifies that a numeric real literal should be a Decimal.

This is actually important to know, since arithmetic on floating point values (such as Double) is imprecise. For instance:

object decimalValue=(5.32 + 2.23);

Here, decimalValue will actually contain a Double, with the unexpected value of 7.5500000000000007! If I want 7.55, I could do this:

object decimalValue=(5.32m + 2.23m);

To answer your question about whether there is a more general suffix, m is the only suffix for Decimal in C#. It might stand for money as you mentioned, but they had do use something other than d, since that's used by Double!

Further reading: decimal (C# Reference)

AminM
  • 1,658
  • 4
  • 32
  • 48
Brian MacKay
  • 31,133
  • 17
  • 86
  • 125
  • 1
    Thanks, I have seen and put the msdn-link into my question exactly as you entered your answer. However I accepted the post of Hans Passant because there is a nice list that will help a lot to people who will come to this post because of the suffixes. But thanks definitively. – HCL Jul 17 '10 at 14:29
1

Short answer to Declare Decimal in C#

decimal firstMoney = 141.28m;

O/P: 141.28

decimal secondMoney = 100.00m;

O/P: 100

For more refer MSDN.

Hope helps someone.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • @Thomas , i understand but i just tried to give short answer for those who searching for declaring decimal, should i delete the post ? , you think its not useful ? – Shaiju T Mar 05 '16 at 10:48
  • 3
    *I* see it as not useful *but* I'm far from perfect and it *may* help others. If, in a second read you find it not useful, then you can delete it. If you think that it'll help someone, leave it as is :) – Thomas Ayoub Mar 07 '16 at 08:45
  • 2
    @ThomasAyoub Personally, don't see what this adds that [Hans answer](https://stackoverflow.com/a/3271831/692942) doesn't already cover. – user692942 Oct 10 '17 at 09:10
  • is it possible to do this with a variable? For example: variableM, rather then 1,25M – I try so hard but I cry harder Nov 14 '22 at 13:15
  • @ItrysohardbutIcryharder It should work if `variableM` has decimal value in it. – Shaiju T Nov 14 '22 at 16:02