Decimal.Subtract() Method in C#

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:

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.