C# “M”在十进制值赋值中代表什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10216182/
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-09 12:55:40  来源:igfitidea点击:

What does the "M" stand for in decimal value assignment?

c#

提问by JCisar

MSDN says:

MSDN 说:

"Without the suffix m, the number is treated as a double, thus generating a compiler error."

“没有后缀 m,数字被视为双精度数,从而产生编译器错误。”

What does the "M" in:

“M”是什么意思:

decimal current = 10.99M;

stand for?

代表?

Is it any different than:

有什么不同:

decimal current = (decimal)10.99

采纳答案by Scott Chamberlain

M makes the number a decimal representation in code.

M 使数字在代码中以十进制表示。

To answer the second part of your question, yes they are different.

要回答问题的第二部分,是的,它们是不同的。

decimal current = (decimal)10.99

is the same as

是相同的

double tmp = 10.99;
decimal current = (decimal)tmp;

Now for numbers larger than sigma it should not be a problem but if you meant decimal you should specify decimal.

现在对于大于 sigma 的数字,这应该不是问题,但如果您的意思是十进制,则应指定十进制。



Update:

更新

Wow, i was wrong. I went to go check the IL to prove my point and the compiler optimized it away.

哇,我错了。我去检查 IL 以证明我的观点,编译器对其进行了优化。



Update 2:

更新2

I was right after all!, you still need to be careful. Compare the output of these two functions.

毕竟我是对的!,你还是要小心。比较这两个函数的输出。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Test1());
        Console.WriteLine(Test2());
        Console.ReadLine();
    }

    static decimal Test1()
    {
        return 10.999999999999999999999M;
    }
    static decimal Test2()
    {
        return (decimal)10.999999999999999999999;
    }
}

The first returns 10.999999999999999999999but the seccond returns 11

第一个返回10.999999999999999999999但第二个返回11



Just as a side note, double will get you 15 decimal digits of precisionbut decimal will get you 96 bits of precision with a scaling factor from 0 to 28. So you can represent any number in the range ((-296to 296) / 10(0 to 28))

作为旁注, double 将为您提供15 位十进制数字的精度,但 decimal 将为您提供96 位精度,缩放因子为 0 到 28。所以你可以表示范围内的任何数字 ((-2 96to 2 96) / 10 (0 to 28))

回答by dknaack

Description

描述

decimal current = 10.99M; 

Tells the compiler you want a decimal number.

告诉编译器你想要一个十进制数。

decimal current = (decimal)10.99

Tells the compiler you want to cast your double 10.99to a decimal.

告诉编译器您想将 double10.99转换为小数。

Some say it stands for money. M because you must use decimals in financial applications. You must use decimals because they are more accurate than floating point numbers (double).

有人说它代表金钱。M 因为您必须在金融应用程序中使用小数。您必须使用小数,因为它们比浮点数(双精度)更准确。

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.

The decimal has more significant figures than the double, therefore it can be more precise- it also takes up slightly more memory. Other than certian math or physics-related algorithms, the double or float should do fine.

十进制后缀是 M/m,因为 D/d 已经被 double 占用了。尽管有人建议 M 代表金钱,但 Peter Golde 回忆说,M 只是被选为十进制中的下一个最佳字母。

十进制比双精度有更多的有效数字,因此它可以更精确——它也占用更多的内存。除了特定的数学或物理相关算法之外,double 或 float 应该可以。

For example

例如

  • If you do ANYNUMBER / 0.5d you will not get the half of ANYNUMBER.
  • If you do ANYNUMBER / 0.5m you will get everytime the half of ANYNUMBER.

  • Use decimalfor money

  • Use doublefor exchange rates
  • 如果你做 ANYNUMBER / 0.5d 你不会得到 ANYNUMBER 的一半。
  • 如果你做 ANYNUMBER / 0.5m 你会得到 ANYNUMBER 的一半。

  • decimal

  • 使用double的汇率

More Information

更多信息

回答by Ed S.

Well... I think "D" would be confused with double, so... they chose "M"? Maybe for "Money"? Seems a bit silly... but I couldn't find anything definitive.

嗯...我认为“D”会与 混淆double,所以...他们选择了“M”?也许是为了“钱”?看起来有点傻……但我找不到任何确定的东西。