Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. This method effectively changes the sign of a decimal number − positive numbers become negative and negative numbers become positive.
Syntax
Following is the syntax −
public static decimal Negate(decimal val);
Parameters
- val − The decimal value to negate.
Return Value
Returns a decimal number that is equal to val multiplied by negative one (-1). If val is positive, the result is negative. If val is negative, the result is positive.
Using Decimal.Negate() with Positive Values
The following example demonstrates how Decimal.Negate() converts a positive decimal value to its negative equivalent −
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);
}
}
The output of the above code is −
Decimal Value = 3972.491 HashCode = 620041307 TypeCode = Decimal Negative (Decimal) = -3972.491
Using Decimal.Negate() with Negative Values
When applied to a negative decimal value, Decimal.Negate() returns the positive equivalent −
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);
}
}
The output of the above code is −
Decimal Value = -29.35 HashCode = 1503969289 TypeCode = Decimal Negative (Decimal) = 29.35
Practical Example with Zero and Multiple Values
Here's an example showing the behavior of Decimal.Negate() with different types of decimal values including zero −
using System;
public class Demo {
public static void Main() {
decimal[] values = { 125.75m, -89.50m, 0.0m, 1000000.99m };
Console.WriteLine("Original\tNegated");
Console.WriteLine("--------\t-------");
foreach (decimal value in values) {
decimal negated = Decimal.Negate(value);
Console.WriteLine("{0}\t\t{1}", value, negated);
}
}
}
The output of the above code is −
Original Negated -------- ------- 125.75 -125.75 -89.50 89.50 0.0 0.0 1000000.99 -1000000.99
Conclusion
The Decimal.Negate() method provides a clean way to change the sign of decimal values in C#. It's equivalent to multiplying by -1 but offers better readability and is specifically designed for decimal operations, making it useful in financial calculations and other precision-critical applications.
