Open In App

Decimal.Subtract() Method in C#

Last Updated : 31 Jan, 2019
Improve
Improve
Like Article
Like
Save
Share
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:




// 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




// 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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads