M 在 C# 十进制文字表示法中代表什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/977484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:43:12  来源:igfitidea点击:

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

c#decimalliterals

提问by coson

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?

M部分代表什么?

采纳答案by Jon Skeet

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):

正如其他人所说,这意味着它是十进制文字。但是,起源可能不是本答案中其他地方建议的起源。来自C# Annotated Standard(ECMA 版本,而不是 MS 版本):

The decimalsuffix 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.

decimal后缀是M /因为d / d已经采取米double。尽管有人建议 M 代表金钱,但 Peter Golde 回忆说,M 只是被选为 中的下一个最好的字母decimal

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

一个类似的注释提到 C# 的早期版本分别包括“Y”和“S”,分别代表byteshort文字。它们因不经常使用而被丢弃。

回答by MDStephens

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

M 指的是“十进制”中的第一个非歧义字符。如果您不添加它,该数字将被视为双倍。

D is double.

D 是双重的。

回答by LuckyBrain

A real literal suffixed by M or m is of type decimal. 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.

以 M 或 m 为后缀的实数是十进制类型。例如,文字 1m、1.5m、1e10m 和 123.456M 都是十进制类型。该文字通过取精确值转换为十进制值,并在必要时使用银行家舍入法四舍五入到最接近的可表示值。除非值被四舍五入或值为零(在后一种情况下,符号和比例将为 0),否则文字中任何明显的比例都会被保留。因此,文字 2.900m 将被解析为带符号 0、系数 2900 和比例 3 的小数。

Read more about real literals

阅读有关真实文字的更多信息

回答by Shadi Namrouti

From C# specifications:

从 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

Note that you can use an uppercase or lowercase notation.

请注意,您可以使用大写或小写表示法。

回答by Ashvani mishra

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

好吧,我猜 M 代表尾数。小数可以用来省钱,但不代表小数只能用来省钱。