MathF.Sign() Method in C# with Examples

In C#, MathF.Sign(Single) is a MathF class method which returns an integer that specify the sign of the number.

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

Return Type: This method returns the value of type System.Int32 as per following mentioned conditions:



Return Value Condition:
0 If value is equal to zero
1 If value is greater than zero
-1 If value is lesser than zero

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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";
    }
}

chevron_right


Output:

Greater than zero
Less than zero


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.