Fetching contributors…
Cannot retrieve contributors at this time
101 lines (69 sloc) 4.81 KB
title f1_keywords helpviewer_keywords ms.assetid
decimal keyword (C# Reference)
decimal keyword [C#]

decimal (C# Reference)

The decimal keyword indicates a 128-bit data type. Compared to other floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations. The approximate range and precision for the decimal type are shown in the following table.

Type Approximate Range Precision .NET type
decimal (-7.9 x 1028 to 7.9 x 1028) / (100 to 1028) 28-29 significant digits xref:System.Decimal?displayProperty=nameWithType

The default value of a decimal is 0m.


If you want a numeric real literal to be treated as decimal, use the suffix m or M, for example:

decimal myMoney = 300.5m;

Without the suffix m, the number is treated as a double and generates a compiler error.


The integral types are implicitly converted to decimal and the result evaluates to decimal. Therefore you can initialize a decimal variable using an integer literal, without the suffix, as follows:

decimal myMoney = 300;

There is no implicit conversion between other floating-point types and the decimal type; therefore, a cast must be used to convert between these two types. For example:

decimal myMoney = 99.9m;
double x = (double)myMoney;
myMoney = (decimal)x;

You can also mix decimal and numeric integral types in the same expression. However, mixing decimal and other floating-point types without a cast causes a compilation error.

For more information about implicit numeric conversions, see Implicit Numeric Conversions Table.

For more information about explicit numeric conversions, see Explicit Numeric Conversions Table.

Formatting decimal output

You can format the results by using the String.Format method, or through the xref:System.Console.Write%2A?displayProperty=nameWithType method, which calls String.Format(). The currency format is specified by using the standard currency format string "C" or "c," as shown in the second example later in this article. For more information about the String.Format method, see xref:System.String.Format%2A?displayProperty=nameWithType.


The following example causes a compiler error by trying to add double and decimal variables.

decimal dec = 0m;
double dub = 9;
// The following line causes an error that reads "Operator '+' cannot be applied to
// operands of type 'double' and 'decimal'"
Console.WriteLine(dec + dub);

// You can fix the error by using explicit casting of either operand.
Console.WriteLine(dec + (decimal)dub);
Console.WriteLine((double)dec + dub);

The result is the following error:

Operator '+' cannot be applied to operands of type 'double' and 'decimal'

In this example, a decimal and an int are mixed in the same expression. The result evaluates to the decimal type.



In this example, the output is formatted by using the currency format string. Notice that x is rounded because the decimal places exceed $0.99. The variable y, which represents the maximum exact digits, is displayed exactly in the correct format.


C# language specification


See also

C# Reference
C# Programming Guide
C# Keywords
Integral Types Table
Built-In Types Table
Implicit Numeric Conversions Table
Explicit Numeric Conversions Table
Standard Numeric Format Strings