Phương thức Decimal.Negate() trong C# được sử dụng để trả về kết quả của việc nhân giá trị Decimal được chỉ định với âm một.
Cú pháp
Dưới đây là cú pháp:
public static decimal Negate (decimal val);
Trong đó, val là giá trị cần đảo dấu.
Ví dụ 1
Hãy xem ví dụ sau để triển khai phương thức Decimal.Negate():
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);
}
}
Đầu ra
Chương trình sẽ cho ra kết quả sau:
Decimal Value = 3972.491 HashCode = 620041307 TypeCode = Decimal Negative (Decimal) = -3972.491
Ví dụ 2
Ví dụ khác triển khai phương thức Decimal.Negate() với giá trị âm:
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);
}
}
Đầu ra
Chương trình sẽ cho ra kết quả sau:
Decimal Value = -29.35 HashCode = 1503969289 TypeCode = Decimal Negative (Decimal) = 29.35