Manipulate decimals with numeric operators in C#

CsharpProgrammingServer Side Programming

With C#, you can manipulate decimals with operators such as _+, - *, etc.

Let us see how to subtract decimal values.

Firstly, set two decimal values −

decimal d1 = 9.5M;
decimal d2 = 4.2M;

Now to subtract the two values −

d1 = d1 - d2;

The following is the code −

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      decimal d1 = 9.5M;
      decimal d2 = 4.2M;
      d1 = d1 + d2;
      Console.WriteLine("Addition of Decimals: "+d1);
      d1 = d1 - d2;
      Console.WriteLine("Subtraction of Decimals: "+d1);
   }
}

Output

Addition of Decimals: 13.7
Subtraction of Decimals: 9.5
raja
Published on 05-Sep-2018 10:49:30
Advertisements