MathF.Sign() Method in C# with Examples

CsharpServer Side ProgrammingProgramming

The MathF.Sign() method in C# returns an integer specifying the sign of the number.

Syntax

Following is the syntax −

public static int Sign (float val);

Above, Val is the floating-point number whose sign is to be calculated.

The return value is 0 if Val is zero. It’s -1 if the value is less than zero and 1 if the value is greater than zero.

Example

Let us now see an example to implement the MathF.Sign() method −

using System;
public class Demo {
   public static void Main(){
      float val1 = 10.23898f;
      float val2 = -20.878788f;
      Console.WriteLine("MathF.Sign(val1) = "+MathF.Sign(val1));
      Console.WriteLine("MathF.Sign(val2) = "+MathF.Sign(val2));
   }
}

Output

This will produce the following output −

MathF.Sign(val1) = 1
MathF.Sign(val2) = -1

Example

Let us now see another example to implement the MathF.Sign() method −

using System;
public class Demo {
   public static void Main(){
      float val1 = 0.00f;
      float val2 = -10.6735f;
      Console.WriteLine("MathF.Sign(val1) = "+MathF.Sign(val1));
      Console.WriteLine("MathF.Sign(val2) = "+MathF.Sign(val2));
   }
}

Output

This will produce the following output −

MathF.Sign(val1) = 0
MathF.Sign(val2) = -1
raja
Published on 12-Nov-2019 09:53:12
Advertisements