Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaration suffix for decimal type

Tags:

c#

.net

People also ask

How do you declare decimals?

To initialize a decimal variable, use the suffix m or M. Like as, decimal x = 300.5m;. If the suffix m or M will not use then it is treated as double. Character Types : The character types represents a UTF-16 code unit or represents the 16-bit Unicode character.

How do I declare a decimal in SQL?

In standard SQL, the syntax DECIMAL( M ) is equivalent to DECIMAL( M ,0) . Similarly, the syntax DECIMAL is equivalent to DECIMAL( M ,0) , where the implementation is permitted to decide the value of M . MySQL supports both of these variant forms of DECIMAL syntax. The default value of M is 10.

What keyword is used for decimal numbers?

Next, float, double, and the combination long double are used to represent numbers with decimal points.

What is decimal in C#?

In C#, Decimal Struct class is used to represent a decimal floating-point number. The range of decimal numbers is +79,228,162,514,264,337,593,543,950,335 to -79,228,162,514,264,337,593,543,950,335.


Documented in the C# language specification, chapter 2.4.4:

float f = 1.2f;
double d = 1.2d;
uint u = 2u;
long l = 2L;
ulong ul = 2UL;
decimal m = 2m;

Nothing for int, byte, sbyte, short, ushort.


Without a suffix, a numerical real literal will be a Double. The m suffix specifies that a numeric real literal should be a Decimal.

This is actually important to know, since arithmetic on floating point values (such as Double) is imprecise. For instance:

object decimalValue=(5.32 + 2.23);

Here, decimalValue will actually contain a Double, with the unexpected value of 7.5500000000000007! If I want 7.55, I could do this:

object decimalValue=(5.32m + 2.23m);

To answer your question about whether there is a more general suffix, m is the only suffix for Decimal in C#. It might stand for money as you mentioned, but they had do use something other than d, since that's used by Double!

Further reading: decimal (C# Reference)


Short answer to Declare Decimal in C#

decimal firstMoney = 141.28m;

O/P: 141.28

decimal secondMoney = 100.00m;

O/P: 100

For more refer MSDN.

Hope helps someone.