Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Decimal and decimal

Tags:

c#

class

If someone could explain to me the difference between Decimal and decimal in C# that would be great.

In a more general fashion, what is the difference between the lower-case structs like decimal, int, string and the upper case classes Decimal, Int32, String.

Is the only difference that the upper case classes also wrap functions (like Decimal.Divide())?

like image 876
Matthew Jones Avatar asked May 27 '09 14:05

Matthew Jones


People also ask

What is difference between decimal and double?

Double (aka double): A 64-bit floating-point number. Decimal (aka decimal): A 128-bit floating-point number with a higher precision and a smaller range than Single or Double.

What is the difference between a float and a decimal?

Float stores an approximate value and decimal stores an exact value. In summary, exact values like money should use decimal, and approximate values like scientific measurements should use float. When multiplying a non integer and dividing by that same number, decimals lose precision while floats do not.

Should I use float or double in C#?

Use float or double ? The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.

What does M mean in decimal C#?

From the C# Annotated Standard (the ECMA version, not the MS version): 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 .


2 Answers

They are the same. The type decimal is an alias for System.Decimal.

So basically decimal is the same thing as Decimal. It's down to user's preference which one to use but most prefer to use int and string as they are easier to type and more familiar among C++ programmers.

like image 193
Martin Avatar answered Sep 17 '22 19:09

Martin


As C# is a .NET language, all types must map to a .NET Framework Type.

To answer your first question, decimal is an Alias of the System.Decimal .NET Framework type. They may be used interchangeably.

To answer your second question, both Decimal and decimal should extend the same functions, including both from the created variable and from the "Structure" of the value type itself.

decimal FirstDec = 12; Decimal SecondDec = 13; decimal ThirdDec = decimal.Ceiling(FirstDec, SecondDec); Decimal FourthDec = Decimal.Floor(ThirdDec); bool isEqual = FirstDec.Equals(SecondDec) && FourthDec.Equals(ThirdDec); 

The following MSDN Page for Built-In Types will show you which System.ValueType each alias maps to. And for Decimal and decimal specifically, you can reference this MSDN Page for Decimal.

like image 23
OmniSECT Avatar answered Sep 21 '22 19:09

OmniSECT