Decimal.Subtract() Method in C#

The Decimal.Subtract() method in C# is used to subtract two specified decimal values and returns the result. This static method provides a way to perform decimal subtraction with high precision, making it ideal for financial and monetary calculations.

Syntax

Following is the syntax −

public static decimal Subtract(decimal val1, decimal val2);

Parameters

  • val1 − The minuend (the number from which another number is to be subtracted).

  • val2 − The subtrahend (the number that is to be subtracted).

Return Value

Returns a decimal value that represents the result of val1 - val2.

Using Decimal.Subtract() Method

Example

Let us see an example to implement the Decimal.Subtract() method −

using System;

public class Demo {
    public static void Main() {
        decimal val1 = 3.45m;
        decimal val2 = 2.35m;
        Console.WriteLine("Decimal 1 = " + val1);
        Console.WriteLine("Decimal 2 = " + val2);
        decimal res = Decimal.Subtract(val1, val2);
        Console.WriteLine("Result (Subtract) = " + res);
    }
}

The output of the above code is −

Decimal 1 = 3.45
Decimal 2 = 2.35
Result (Subtract) = 1.10

Using Decimal.Subtract() with Large Values

Example

The following example demonstrates subtracting larger decimal values −

using System;

public class Demo {
    public static void Main() {
        decimal val1 = 1000000.50m;
        decimal val2 = 250000.25m;
        Console.WriteLine("Decimal 1 = " + val1);
        Console.WriteLine("Decimal 2 = " + val2);
        decimal res = Decimal.Subtract(val1, val2);
        Console.WriteLine("Result (Subtract) = " + res);
    }
}

The output of the above code is −

Decimal 1 = 1000000.50
Decimal 2 = 250000.25
Result (Subtract) = 750000.25

Handling Overflow Exceptions

Example

When the result of subtraction exceeds the range of the decimal type, an OverflowException is thrown. The following example shows how to handle this scenario −

using System;

public class Demo {
    public static void Main() {
        try {
            decimal val1 = 2.35m;
            decimal val2 = Decimal.MinValue;
            Console.WriteLine("Decimal 1 = " + val1);
            Console.WriteLine("Decimal 2 = " + val2);
            decimal res = Decimal.Subtract(val1, val2);
            Console.WriteLine("Result (Subtract) = " + res);
        }
        catch (OverflowException ex) {
            Console.WriteLine("OverflowException: " + ex.Message);
        }
    }
}

The output of the above code is −

Decimal 1 = 2.35
Decimal 2 = -79228162514264337593543950335
OverflowException: Value was either too large or too small for a Decimal.

Conclusion

The Decimal.Subtract() method provides precise decimal subtraction for financial calculations. It throws an OverflowException when the result exceeds the decimal range, so proper exception handling should be implemented when working with extreme values.

Updated on: 2026-03-17T07:04:35+05:30

652 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements