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.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources