Decimal.Negate() Method in C#


The Decimal.Negate() method in C# is used to return the result of multiplying the specified Decimal value by negative one.

Syntax

Following is the syntax −

public static decimal Negate (decimal val);

Above, Val is the value to negate.

Example

Let us now see an example to implement the Decimal.Negate() method −

using System;
public class Demo {
   public static void Main(){
      Decimal val = 3972.491M;
      Console.WriteLine("Decimal Value = {0}", val);
      Console.WriteLine("HashCode = {0}", (val.GetHashCode()) );
      TypeCode type = val.GetTypeCode();
      Console.WriteLine("TypeCode = "+type);
      Decimal res = Decimal.Negate(val);
      Console.WriteLine("Negative (Decimal) = "+res);
   }
}

Output

This will produce the following output −

Decimal Value = 3972.491
HashCode = 620041307
TypeCode = Decimal
Negative (Decimal) = -3972.491

Example

Let us now see another example to implement the Decimal.Negate() method −

using System;
public class Demo {
   public static void Main(){
      Decimal val = -29.35m;
      Console.WriteLine("Decimal Value = {0}", val);
      Console.WriteLine("HashCode = {0}", (val.GetHashCode()) );
      TypeCode type = val.GetTypeCode();
      Console.WriteLine("TypeCode = "+type);
      Decimal res = Decimal.Negate(val);
      Console.WriteLine("Negative (Decimal) = "+res);
   }
}

Output

This will produce the following output −

Decimal Value = -29.35
HashCode = 1503969289
TypeCode = Decimal
Negative (Decimal) = 29.35

Updated on: 13-Nov-2019

580 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements