.NET API Docs
System.Decimal Structure
Namespace: System
Assembly: mscorlib
Inheritance: Object → ValueType → Decimal
Implements: IComparable, IComparable<Decimal>, IConvertible, IEquatable<Decimal>
Assembly: mscorlib
Inheritance: Object → ValueType → Decimal
Implements: IComparable, IComparable<Decimal>, IConvertible, IEquatable<Decimal>
Represents a decimal floating-point number.
Fields
Name | Description | |
---|---|---|
static readonly | MinValue | The minimum value of a Decimal . |
static readonly | MaxValue | The maximum value of a Decimal . |
static readonly | Zero | Represents the number zero. |
static readonly | One | Represents the number one. |
static readonly | MinusOne | Represents the number negative one. |
static readonly | DecimalPlaces | Represents the maximum number of decimal places for a Decimal value. |
Constructors
Name | Description | |
---|---|---|
public | Decimal(Int32[]) | Initializes a new instance of the Decimal structure by using an array of integers. |
public | Decimal(Int64) | Initializes a new instance of the Decimal structure by using a 64-bit signed integer. |
public | Decimal(Double) | Initializes a new instance of the Decimal structure by using a double-precision floating-point number. |
public | Decimal(Single) | Initializes a new instance of the Decimal structure by using a single-precision floating-point number. |
Methods
Name | Description | |
---|---|---|
public static | Abs | Returns the absolute value of a Decimal number. |
public static | Add | Adds two Decimal values and returns the resulting Decimal . |
public static | Compare | Compares two Decimal values and returns an integer that indicates whether the first value precedes, is equal to, or follows the second value in sort order. |
public static | Parse | Converts the string representation of a number to its Decimal equivalent. |
public static | Floor | Computes the integral part of a Decimal number. |
public static | Round | Rounds a Decimal number to the nearest integral value. |
Properties
Name | Description | |
---|---|---|
public | sign | Returns a value indicating the sign of the Decimal number. |
public | GetBits | Returns a 32-bit integer array that contains the service bits and the three 32-bit integers that store the value of this instance. |
Examples
Basic Usage
decimal price = 19.99m;
decimal quantity = 3m;
decimal total = price * quantity;
Console.WriteLine($"Total cost: {total:C}"); // Example: Total cost: $59.97
Precision and Accuracy
The Decimal
type is ideal for financial and monetary calculations due to its high precision and lack of rounding errors inherent in binary floating-point types like double
.
double d1 = 0.1;
double d2 = 0.2;
double sumDouble = d1 + d2; // May not be exactly 0.3
decimal m1 = 0.1m;
decimal m2 = 0.2m;
decimal sumDecimal = m1 + m2; // Will be exactly 0.3m
Console.WriteLine($"Double sum: {sumDouble}");
Console.WriteLine($"Decimal sum: {sumDecimal}");