System.Math.Sign 方法 (Int64)

方法描述

返回一个值,表示 64 位有符号整数的符号。

语法定义(C# System.Math.Sign 方法 (Int64) 的用法)

public static int Sign(
	long value
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-Int64 有符号的数字。
返回值 System.Int32 一个指示 value 的符号的数字,如下表所示。 含义 -1 value 小于零。 0 value 等于零。 1 value 大于零。

提示和注释

System.Math.Sign 方法 (Int64)例子

下面的示例演示如何使用 Sign(Int64) 方法确定 Int64 值的符号并将其显示到控制台。

// This example demonstrates Math.Sign()
using System;

class Sample 
{
    public static void Main() 
    {
    string str = "{0}: {1,3} is {2} zero.";
    string nl = Environment.NewLine;

    byte     xByte1    = 0;	
    short    xShort1   = -2;
    int      xInt1     = -3;
    long     xLong1    = -4;
    float    xSingle1  = 0.0f;
    double   xDouble1  = 6.0;
    Decimal  xDecimal1 = -7m;

// The following type is not CLS-compliant.
    sbyte    xSbyte1   = -101;

    Console.WriteLine("{0}Test the sign of the following types of values:", nl);
    Console.WriteLine(str, "Byte   ", xByte1, Test(Math.Sign(xByte1)));
    Console.WriteLine(str, "Int16  ", xShort1, Test(Math.Sign(xShort1)));
    Console.WriteLine(str, "Int32  ", xInt1, Test(Math.Sign(xInt1)));
    Console.WriteLine(str, "Int64  ", xLong1, Test(Math.Sign(xLong1)));
    Console.WriteLine(str, "Single ", xSingle1, Test(Math.Sign(xSingle1)));
    Console.WriteLine(str, "Double ", xDouble1, Test(Math.Sign(xDouble1)));
    Console.WriteLine(str, "Decimal", xDecimal1, Test(Math.Sign(xDecimal1)));
//
    Console.WriteLine("{0}The following type is not CLS-compliant.", nl);
    Console.WriteLine(str, "SByte  ", xSbyte1, Test(Math.Sign(xSbyte1)));
    }
//
    public static String Test(int compare)
    {
    if (compare == 0) 
       return "equal to";
    else if (compare < 0)  
        return "less than";
    else 
        return "greater than";
    }
}
/*
This example produces the following results:

Test the sign of the following types of values:
Byte   :   0 is equal to zero.
Int16  :  -2 is less than zero.
Int32  :  -3 is less than zero.
Int64  :  -4 is less than zero.
Single :   0 is equal to zero.
Double :   6 is greater than zero.
Decimal:  -7 is less than zero.

The following type is not CLS-compliant.
SByte  : -101 is less than zero.
*/

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。