Open In App

Decimal.Subtract() Method in C#

Last Updated : 31 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
This method is used to subtract the one specified Decimal value from another.
Syntax: public static decimal Subtract (decimal a1, decimal a2); Parameters: a1: This parameter specifies the minuend. a2: This parameter specifies the subtrahend. Return Value: Result of subtracting a2 from a1.
Exceptions: This method will give OverflowException if the return value is less than MinValue or greater than MaxValue. Below programs illustrate the use of the above-discussed method: Example 1: csharp
// C# program to demonstrate the
// Decimal.Subtract(Decimal, Decimal)
// Method
using System;

class GFG {

    // Main Method
    public static void Main()
    {
        try {

            // Declaring the decimal
            // variables
            Decimal a1 = .01m;
            Decimal a2 = .03m;

            // Subtracting the two Decimal value
            // using Subtract() method
            Decimal value = Decimal.Subtract(a1, a2);

            // Display the Result
            Console.WriteLine("Result is : {0}",
                                         value);
        }

        catch (OverflowException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
Output:
Result is : -0.02
Example 2: For OverflowException csharp
// C# program to demonstrate the
// Decimal.Subtract(Decimal, Decimal)
// Method
using System;

class GFG {

    // Main Method
    public static void Main()
    {
        try {

            // Declaring the decimal variables
            Decimal a1 = Decimal.MinValue;
            Decimal a2 = Decimal.MaxValue;

            // Subtracting the two Decimal value
            // using Subtract() method;
            Decimal value = Decimal.Subtract(a1, a2);

            // Display the Result
            Console.WriteLine("Result is : {0}",
                                        value);
        }

        catch (OverflowException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
Output:
Exception Thrown: System.OverflowException
Reference:

Next Article

Similar Reads