> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# decimal

The `decimal` keyword in C# is an alias for the .NET `System.Decimal` struct. It is a 128-bit (16-byte) value type that provides high-precision, base-10 floating-point arithmetic. Unlike base-2 floating-point types (`float` and `double`), `decimal` accurately represents base-10 fractions, eliminating the binary rounding errors inherent in IEEE 754 standard floating-point representations.

## Technical Specifications

* **Size:** 16 bytes (128 bits)
* **Precision:** 28 to 29 significant digits
* **Range:** $\pm 1.0 \times 10^{-28}$ to $\pm 7.9228 \times 10^{28}$
* **Default Value:** `0m`
* **Underlying .NET Type:** `System.Decimal`

## Syntax and Initialization

To initialize a `decimal` with a real number literal, you must append the `m` or `M` suffix. Without the suffix, the compiler treats real number literals as `double`, resulting in a type mismatch error.

```csharp theme={"dark"}
// Correct initialization using the 'm' suffix
decimal pi = 3.14159265358979323846m;

// Implicit conversion from integer types is allowed
decimal wholeNumber = 42; 

// CS0664: Cannot implicitly convert type 'double' to 'decimal'
// decimal errorValue = 3.14; 

// Explicit casting is required when converting from floating-point types
decimal castValue = (decimal)3.14;
```

## Trailing Zeros and Scaling

A unique semantic detail of the C# `decimal` type is that it preserves trailing zeros in its internal representation. The literals `1.5m` and `1.50m` possess different internal scaling factors and will output differently when `.ToString()` is called. However, the equality operator normalizes these values, meaning they evaluate as logically equal.

```csharp theme={"dark"}
decimal val1 = 1.5m;
decimal val2 = 1.50m;

Console.WriteLine(val1 == val2);       // Output: True
Console.WriteLine(val1.ToString());    // Output: 1.5
Console.WriteLine(val2.ToString());    // Output: 1.50
```

## Internal Memory Layout

The 128 bits of a `decimal` instance are not structured like standard IEEE 754 floating-point numbers. Instead, the memory is partitioned into four 32-bit integers, representing the following components:

1. **Mantissa (Coefficient):** 96 bits (the first three 32-bit integers) representing an unsigned integer value.
2. **Sign:** 1 bit (bit 31, the highest bit of the fourth 32-bit integer) indicating positive (`0`) or negative (`1`).
3. **Exponent (Scaling Factor):** 8 bits (bits 16–23 of the fourth 32-bit integer) representing an integer from 0 to 28. This defines the implicitly negative base-10 exponent, effectively determining the position of the decimal point.
4. **Reserved:** 23 bits (bits 0–15 and 24–30 of the fourth 32-bit integer) are unused and must be zero.

The mathematical value of a `decimal` is evaluated as:

`(-1)^sign * mantissa / 10^exponent`

## Type Conversions

Because `decimal` has a different internal architecture and precision scale compared to other numeric types, conversion rules are strict:

* **Implicit Conversions:** Allowed from integral types (`sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `char`).
* **Explicit Conversions:** Required when converting to or from `float` and `double`. Converting from `float`/`double` to `decimal` can throw an `OverflowException` if the value exceeds the `decimal` range.
* **No Implicit Floating-Point Conversion:** `decimal` cannot be implicitly converted to `float` or `double`. While `float` and `double` have significantly larger ranges than `decimal` (up to $\approx 10^{38}$ and $10^{308}$ respectively), `decimal` has higher precision. The conversion is strictly explicit to prevent the unintentional loss of precision and exactness that occurs when moving from base-10 to base-2 floating-point representations.

```csharp theme={"dark"}
int intVal = 100;
decimal decVal = intVal; // Implicit conversion

double doubleVal = 1.234e5;
decimal convertedDec = (decimal)doubleVal; // Explicit conversion required

decimal largeDec = 12345.6789m;
float floatVal = (float)largeDec; // Explicit conversion required (precision lost)
```

## Special Values and Exceptions

Unlike base-2 floating-point types, `decimal` does not support or represent `NaN` (Not a Number), `PositiveInfinity`, or `NegativeInfinity`. Consequently, operations that would result in infinity or undefined values in IEEE 754 arithmetic will instead throw exceptions in C#. For example, division by zero throws a `DivideByZeroException`, while arithmetic overflows (which result in infinity in IEEE 754) throw an `OverflowException`.

## Performance Characteristics

Modern CPUs process base-2 floating-point arithmetic (`float`, `double`) natively via the hardware Floating-Point Unit (FPU). Because the CPU lacks native hardware instructions for 128-bit base-10 arithmetic, `decimal` operations are implemented purely in software within the .NET runtime libraries. Consequently, arithmetic operations, comparisons, and conversions involving `decimal` are significantly slower and consume more memory bandwidth than their `double` or `float` counterparts.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
