442

In order to work with decimal data types, I have to do this with variable initialization:

decimal aValue = 50.0M;

What does the M part stand for?

Igby Largeman
  • 16,495
  • 3
  • 60
  • 86
coson
  • 8,301
  • 15
  • 59
  • 84

5 Answers5

527

It means it's a decimal literal, as others have said. However, the origins are probably not those suggested elsewhere in this answer. From the C# Annotated Standard (the ECMA version, not the MS version):

The decimal suffix is M/m since D/d was already taken by double. Although it has been suggested that M stands for money, Peter Golde recalls that M was chosen simply as the next best letter in decimal.

A similar annotation mentions that early versions of C# included "Y" and "S" for byte and short literals respectively. They were dropped on the grounds of not being useful very often.

zildjohn01
  • 11,339
  • 6
  • 52
  • 58
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 15
    That leaves the question why E/e was not the next best letter. – VVS Jun 10 '09 at 19:14
  • 89
    "e" would be problematic in that it's already used in literals. For instance "2e4m" is a valid literal (decimal 20000). Even if it were unambiguous (and it may well be, although "1e" ends up with a curious compiler error message suggesting it's out of the range of viable doubles, not that it's syntactically invalid; I suspect that's a bug) it would still be confusing. "c" would possibly make sense without causing any confusion, although it would no doubt suggest "currency" to people :) – Jon Skeet Jun 10 '09 at 19:28
  • 11
    "c" conflicts with the character literal for strings in VB.Net, though even there the grammar is a little different. – Joel Coehoorn Jun 10 '09 at 19:39
  • 6
    I wouldn't have too much problem with it being different in C# and VB. VB already has different hex syntax for example, IIRC. – Jon Skeet Jun 10 '09 at 19:40
  • 22
    If you lay out the syllables of "Decimal", you get "De Ci Mal". These make the most sense to use. D is already taken, C could be characters, M seems like a logical choice. – Aaron Franke Feb 28 '18 at 09:57
  • 1
    [This list](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types-table) is a nice overview including the type suffix for decimal. – Kapé Feb 05 '19 at 14:04
  • 1
    The type of a real literal is determined by its suffix. The literal without suffix or with the d or D suffix is of type double. The literal with the f or F suffix is of type float. The literal with the m or M suffix is of type decimal. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types#real-literals – Ahmad Ahsan Feb 28 '21 at 17:32
124

From C# specifications:

var f = 0f; // float
var d = 0d; // double
var m = 0m; // decimal (money)
var u = 0u; // unsigned int
var l = 0l; // long
var ul = 0ul; // unsigned long

Note that you can use an uppercase or lowercase notation.

Erik Humphrey
  • 345
  • 5
  • 18
Shadi Alnamrouti
  • 11,796
  • 4
  • 56
  • 54
34

M refers to the first non-ambiguous character in "decimal". If you don't add it the number will be treated as a double.

D is double.

isherwood
  • 58,414
  • 16
  • 114
  • 157
MDStephens
  • 1,013
  • 2
  • 9
  • 10
  • 6
    I suppose there is some important reason, but it irritates me that you get a compile-time error if you leave the m out of the literal. It's not as if it's ambiguous. – SeaDrive Jul 01 '09 at 20:25
  • 4
    @JSON It works that way for integer types, because there's no ambiguity there - as long as it fits the type, it will not lose/garble any information. The same is not true of decimal numbers. Casting the double value `0.42` to decimal can give you a different value than `0.42M` (and the same for using a double literal in place of a float literal - `float val = 423.3` also fails). So you're choosing between a subtly wrong behavior and a compiler error that takes half a second to fix and conforms to the CLR and C# standards. – Luaan Oct 18 '18 at 08:03
  • 3
    "M", like in "deciMal" – Shadi Alnamrouti Sep 15 '19 at 19:06
21

A real literal suffixed by M or m is of type decimal (money). For example, the literals 1m, 1.5m, 1e10m, and 123.456M are all of type decimal. This literal is converted to a decimal value by taking the exact value, and, if necessary, rounding to the nearest representable value using banker's rounding. Any scale apparent in the literal is preserved unless the value is rounded or the value is zero (in which latter case the sign and scale will be 0). Hence, the literal 2.900m will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3.

Read more about real literals

Lucky Brain
  • 1,551
  • 12
  • 14
-2

Well, I guess M represent the mantissa. Decimal can be used to save money, but it doesn't mean, decimal only used for money.

Rahul Rahatal
  • 648
  • 5
  • 19
  • 3
    That is indeed a guess, and it's much less plausible than the "first available non-ambiguous character" hypothesis. If you have supporting evidence, include it in your answer. – isherwood Dec 09 '19 at 22:19
  • 1
    I guess the reason for the downvotes here would be that this answer may be quite misleading. The "Mantissa" is the thing that is the difference between the decimal type (not having a mantissa) and the double type (having a mantissa). – mattanja Oct 13 '22 at 12:44