Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

decimal
subtract
c#
communitycreator

How to subtract one decimal value from another in C#

Theodore Kelechukwu Onyejiaku

Overview

We use the decimal.Subtract() method to subtract one decimal value from another.

Syntax

public static decimal Subtract (decimal d1, decimal d2);

Parameters

  • d1: This is the minuend.
  • d2: This is the subtrahend. We subtract this from d1.

Return value

This method returns the subtraction result of two decimal values.

Example

// use System
using System;

// create class
class DecimalSubtracter
{
    // main method
    static void Main()
    {
      // initialise decimal values
      decimal d1 = 234.3m;
      decimal d2 = 3.5678m;
      decimal d3 = 34.45m;
      decimal d4 = 67.245m;

      // subtract decimal values
      decimal r1 = Decimal.Subtract(d1,d2);
      decimal r2 = Decimal.Subtract(d3,d4);

      // print results
      Console.WriteLine(r1);
      Console.WriteLine(r2);
    }
}
An example of using the Subtract() method

Explanation

  • Line 11–14: We create some decimal values, d1, d2, d3, and d4.

  • Line 17: We subtract d2 from d1 using decimal.Subtract() and store the result in r1.

  • Line 18: We subtract d4 from d3 using the decimal.Subtract() method and store the result in r2.

  • Line 21: We print the result of subtracting d2 from d1.

  • Line 22: We print the result of subtracting d4 from d3.

RELATED TAGS

decimal
subtract
c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring