23

MSDN says:

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

What does the "M" in:

decimal current = 10.99M;

stand for?

Is it any different than:

decimal current = (decimal)10.99
JCisar
  • 2,584
  • 2
  • 29
  • 28
  • 2
    Duplicate of... http://stackoverflow.com/questions/977484/c-sharp-decimal-data-type – scottheckel Apr 18 '12 at 19:06
  • 1
    It's not a duplicate because it does not address the casting part of the question as Scott does below. – Kirk Woll Apr 18 '12 at 19:10
  • Skeet said it all I think so no sense in answering this - just one 'help tip' - `if you type that number in the VS (if that's what you're using) - you'll get the info "Represents a Decimal Number". If you type #D or f etc. you'll get different explanations.` – NSGaga-mostly-inactive Apr 18 '12 at 19:12
  • 1
    @NSGaga, as I already stated, Skeet's answer does not address the question of the cast. Therefore it's not a dup of this question. – Kirk Woll Apr 18 '12 at 19:13
  • @KirkWoll not saying it is, there's always something to add – NSGaga-mostly-inactive Apr 18 '12 at 19:16

3 Answers3

35

M makes the number a decimal representation in code.

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.


Update:

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


Update 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.999999999999999999999 but the seccond returns 11


Just as a side note, double will get you 15 decimal digits of precision but 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 ((-296 to 296) / 10(0 to 28))

Boivie
  • 3
  • 2
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
10

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.99 to a decimal.

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

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.

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 decimal for money

  • Use double for exchange rates

More Information

Community
  • 1
  • 1
dknaack
  • 60,192
  • 27
  • 155
  • 202
2

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.

Ed S.
  • 122,712
  • 22
  • 185
  • 265