为了使用十进制数据类型,我必须对变量初始化这样做:

decimal aValue = 50.0M;

M部分代表什么?


M是十进制中第一个无歧义的字符。如果你不加,这个数字将被视为双数。

D是二阶的。

就像其他人说的,这意味着它是一个十进制的字面量。然而,起源可能不是这个答案中其他地方提到的那些。来自c#注解标准(ECMA版本,而不是MS版本):

十进制后缀为M/ M,因为D/ D 已经被加倍了。 尽管有人认为M 代表钱,彼得·戈德回忆道 M被简单地选为下一个 十进制中最好的字母。

一个类似的注释提到,c#的早期版本分别包括“Y”和“S”表示字节和短字面量。它们被放弃了,理由是不经常有用。

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.

阅读更多关于真实文字的内容

来自c#规范:

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

注意,您可以使用大写或小写符号。

我猜M代表尾数。十进制可以用来省钱,但这并不意味着十进制只用于赚钱。