What is the Decimal.Negate() method in C#?

Overview

The Decimal.Negate() method is a C# method that is used to get the result of a decimal value when it is multiplied by a negative value (-1).

Syntax

public static decimal Negate (decimal d);

Parameters

  • d: The decimal value that we want to multiply by a negative value.

Return value

The Decimal.Negate() method returns a decimal number that is the negative of d. If d is zero, then the method returns a zero.

Code example

// use system
using System;
// create class
class NegateDecimal
{
// main method
static void Main()
{
// negate decimal values
Console.WriteLine(Decimal.Negate(334.45m));
Console.WriteLine(Decimal.Negate(-7.899m));
Console.WriteLine(Decimal.Negate(-1.323m));
}
}

Code explanation

  • Line 10: We negate the decimal value 334.45m and print the result.
  • Line 11: We negate -7.899m and print the result.
  • Line 12: We use the Decimal.Negate() method to negate -1.323m and print the result.

When we run the code, we get the negative values of the decimal numbers that we negated.

Free Resources