當前位置: 首頁>>代碼示例>>C#>>正文


C# Math.Sign方法代碼示例

本文整理匯總了C#中System.Math.Sign方法的典型用法代碼示例。如果您正苦於以下問題:C# Math.Sign方法的具體用法?C# Math.Sign怎麽用?C# Math.Sign使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Math的用法示例。


在下文中一共展示了Math.Sign方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

// 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($"{nl}Test the sign of the following types of values:");
        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($"{nl}The following type is not CLS-compliant.");
        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";
    }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:43,代碼來源:Math.Sign

輸出:

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.

示例2: Main

//引入命名空間
using System;

public class MainClass {
    public static void Main() {
        Console.WriteLine("Math.Sign(1) = {0}", Math.Sign(1));
        Console.WriteLine("Math.Sign(-1) = {0}", Math.Sign(-1));
        Console.WriteLine("Math.Sign(0) = {0}", Math.Sign(0));
    }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:10,代碼來源:Math.Sign


注:本文中的System.Math.Sign方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。