Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1234.927M -> what is the M??? 5

Status
Not open for further replies.

NoCoolHandle

Programmer
Apr 10, 2003
2,321
0
0
US
I have seem code that looks somthing like

Return 1234.8434M

What does the M reference.. It looks like a datatype declaration, but.....

TIA

Rob
 
it converts the double to a decimal.
this will throw an exception
Code:
decimal myVar = 1234.8434;
this won't
Code:
decimal myVar = 1234.8434M;


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
That represents a decimal number, which has a higher precision than floating.
 
Are there any other cool things you can tuck onto data to do much the same thing?

IE I know I can do a 2345.434D


TIA

Rob
 
f = float
m = decimal

then there's cool things like string formatting

DateTime.Now.ToString("dd/MM/yyyy");
//Outputs 14/05/2007
DateTime.Now.ToString("hh:mm:ss");
//Outputs 12:53:03

Number Formatting

decimal cost = 22.385429;

Console.WriteLine(cost.ToString(N2));
//Write to the console with only 2 decimal places
 
Code:
decimal m1; // good
decimal m2 = 100; // better
decimal m3 = 100M; // best

The declaration of m1 allocates a variable m1 without initializing it to anything.
Until you assign it a value, the contents of m1 are indeterminate. But that’s
okay, because C# doesn’t let you use m1 for anything until you assign it a value.
The second declaration creates a variable m2 and initializes it to a value of 100.
What isn’t obvious is that 100 is actually of type int. Thus, C# must convert
the int into a decimal type before performing the initialization. Fortunately,
C# understands what you mean and performs the conversion for you.
The declaration of m3 is the best. This clever declaration initializes m3 with
the decimal constant 100M. The letter M at the end of the number specifies
that the constant is of type decimal. No conversion is required.[/color green]
 

1 //int
1U //unsigned int
1L //long int
1.0 //double
1.0F //float
1M //decimal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top