當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# MathF.Sign()用法及代碼示例


在C#中,MathF.Sign(Single)是MathF類方法,該方法返回一個整數,該整數指定數字的符號。

用法: public static int Sign (float x);
Here, x is the required single precision floating-point number whose sign has to be calculated.

返回類型:此方法根據以下提到的條件返回System.Int32類型的值:


返回值 條件:
0 如果值等於零
1 如果值大於零
-1 如果值小於零

例:

// C# program to demonstrate the 
// MathF.Sign(Single) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    static void Main(string[] args) 
    { 
  
        // float data type 
        float f1 = 746.89f; 
        float f2 = -782.1247f; 
  
        // displaying result 
        Console.WriteLine(check(MathF.Sign(f1))); 
        Console.WriteLine(check(MathF.Sign(f2))); 
    } 
  
    // function to check whether the input 
    // number is greater than zero or not 
    public static String check(int r) 
    { 
        if (r == 0) 
            return "Equal to zero"; 
  
        else if (r < 0) 
            return "Less than zero"; 
  
        else
            return "Greater than zero"; 
    } 
}
輸出:
Greater than zero
Less than zero


相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 MathF.Sign() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。