📜  C#中的Decimal.Negate()方法

📅  最后修改于: 2021-05-30 00:59:50             🧑  作者: Mango

此方法用于获得将指定的十进制值乘以负1的结果。

下面的程序说明了Decimal.Negate(Decimal)方法的用法:

示例1:当a为正时

// C# program to demonstrate the
// Decimal.Negate(Decimal) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring the decimal variable
        Decimal a = 127.97m;
  
        // using Negate() method;
        Decimal value = Decimal.Negate(a);
  
        // Display the negative value
        Console.WriteLine("The negative value "+
                             "is : {0}", value);
    }
}
输出:
The negative value is : -127.97

示例2:当a为负数时

// C# program to demonstrate the
// Decimal.Negate(Decimal) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring the decimal variable
        Decimal a = -12.39m;
  
        // using Negate() method;
        Decimal value = Decimal.Negate(a);
  
        // Display the value after
        // using negate method
        Console.WriteLine("The value is : {0}",
                                        value);
    }
}
输出:
The value is : 12.39

示例3:如果a为零。

// C# program to demonstrate the
// Decimal.Negate(Decimal) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Declaring the decimal variable
        Decimal a = 0.00m;
  
        // using Negate() method;
        Decimal value = Decimal.Negate(a);
  
        // Display the Negate value
        Console.WriteLine("The Negate value "+
                           "is : {0}", value);
    }
}
输出:
The Negate value is : 0.00

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.decimal.negate?view=netframework-4.7.2