What does the M stand for in C# Decimal literal notation?
Asked Answered
I

6

458

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?

Illegal answered 10/6, 2009 at 18:50 Comment(0)
F
539

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.

Fleuron answered 10/6, 2009 at 19:5 Comment(7)
That leaves the question why E/e was not the next best letter.Rapeseed
"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 :)Fleuron
"c" conflicts with the character literal for strings in VB.Net, though even there the grammar is a little different.Isabellaisabelle
I wouldn't have too much problem with it being different in C# and VB. VB already has different hex syntax for example, IIRC.Fleuron
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.Pyroconductivity
This list is a nice overview including the type suffix for decimal.Gunthar
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. learn.microsoft.com/en-us/dotnet/csharp/language-reference/…Stream
D
130

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.

Dvina answered 16/1, 2019 at 12:34 Comment(0)
A
35

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.

Aleksandropol answered 10/6, 2009 at 18:54 Comment(3)
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.Hillary
@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.Jannajannel
"M", like in "deciMal"Dvina
Z
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

Zeebrugge answered 28/3, 2016 at 0:52 Comment(1)
Thank you for linking to the docs about all of the real literals.Joceline
C
3

"The letter M was chosen as the most visually distinct letter between the double and decimal keywords."

Chekiang answered 29/1 at 0:11 Comment(0)
Y
-4

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

Yb answered 24/2, 2019 at 6:38 Comment(2)
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.Tena
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).Eupatrid

© 2022 - 2024 — McMap. All rights reserved.