Decimal.Subtract() Method in C#
Last Updated :
31 Jan, 2019
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:
using System;
class GFG {
public static void Main()
{
try {
Decimal a1 = .01m;
Decimal a2 = .03m;
Decimal value = Decimal.Subtract(a1, a2);
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
using System;
class GFG {
public static void Main()
{
try {
Decimal a1 = Decimal.MinValue;
Decimal a2 = Decimal.MaxValue;
Decimal value = Decimal.Subtract(a1, a2);
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
Share your thoughts in the comments
Please Login to comment...