C# Math.Sign() – Examples

In this tutorial, we will learn about the C# Math.Sign() method, and learn how to use this method to get the sign of given number, with the help of examples.

Sign(Decimal)

Math.Sign(value) returns an integer that indicates the sign of a decimal number value.

ADVERTISEMENT

Syntax

The syntax of Sign() method is

Math.Sign(Decimal value)

where

ParameterDescription
valueA signed decimal number.

Return Value

The method returns Integer value.

Example 1 – Sign(Decimal)

In this example, we will take some decimal values and find their sign using Math.Sign() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Decimal value;
        int result;

        value = 10.2563M;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");

        value = -10.2563M;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");
    }
}

Output

Sign(10.2563) = 1
Sign(-10.2563) = -1

Sign(Double)

Math.Sign(value) returns an integer that indicates the sign of a double-precision floating-point number value.

Syntax

The syntax of Sign() method is

Math.Sign(Double value)

where

ParameterDescription
valueA signed double-precision floating-point number.

Return Value

The method returns Integer value.

Example 2 – Sign(Double)

In this example, we will take some double-precision floating-point numbers and find their sign using Math.Sign() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Double value;
        int result;

        value = 10.2563;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");

        value = -10.2563;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");
    }
}

Output

Sign(10.2563) = 1
Sign(-10.2563) = -1

Sign(Int16)

Math.Sign(value) returns an integer that indicates the sign of a 16-bit signed integer value.

Syntax

The syntax of Sign() method is

Math.Sign(Int16 value)

where

ParameterDescription
valueA 16-bit signed integer.

Return Value

The method returns Integer value.

Example 3 – Sign(Int16)

In this example, we will take some 16-bit signed integers and find their sign using Math.Sign() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Int16 value;
        int result;

        value = 4;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");

        value = -4;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");
    }
}

Output

Sign(4) = 1
Sign(-4) = -1

Sign(Int32)

Math.Sign(value) returns an integer that indicates the sign of a 32-bit signed integer value.

Syntax

The syntax of Sign() method is

Math.Sign(Int32 value)

where

ParameterDescription
valueA 32-bit signed integer.

Return Value

The method returns Integer value.

Example 4 – Sign(Int32)

In this example, we will take some 32-bit signed integers and find their sign using Math.Sign() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Int32 value;
        int result;

        value = 4;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");

        value = -4;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");
    }
}

Output

Sign(4) = 1
Sign(-4) = -1

Sign(Int64)

Math.Sign(value) returns an integer that indicates the sign of a 64-bit signed integer value.

Syntax

The syntax of Sign() method is

Math.Sign(Int64 value)

where

ParameterDescription
valueA 64-bit signed integer.

Return Value

The method returns Integer value.

Example 5 – Sign(Int64)

In this example, we will take some 64-bit signed integers and find their sign using Math.Sign() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Int64 value;
        int result;

        value = 4;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");

        value = -4;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");
    }
}

Output

Sign(4) = 1
Sign(-4) = -1

Sign(SByte)

Math.Sign(value) returns an integer that indicates the sign of an 8-bit signed integer value.

Syntax

The syntax of Sign() method is

Math.Sign(SByte value)

where

ParameterDescription
SByteAn 8-bit signed integer

Return Value

The method returns Integer value.

Example 6 – Sign(SByte)

In this example, we will take some 8-bit signed integers and find their sign using Math.Sign() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        SByte value;
        int result;

        value = 4;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");

        value = -4;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");
    }
}

Output

Sign(4) = 1
Sign(-4) = -1

Sign(Single)

Math.Sign(value) returns an integer that indicates the sign of a single-precision floating-point number value.

Syntax

The syntax of Sign() method is

Math.Sign(Single value)

where

ParameterDescription
valueA single-precision floating-point number

Return Value

The method returns Integer value.

Example 7 – Sign(Single)

In this example, we will take some single-precision floating-point numbers and find their sign using Math.Sign() method.

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Single value;
        int result;

        value = 4.2F;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");

        value = -4.2F;
        result = Math.Sign(value);
        Console.WriteLine($"Sign({value}) = {result}");
    }
}

Output

Sign(4.2) = 1
Sign(-4.2) = -1

Conclusion

In this C# Tutorial, we have learnt the syntax of C# Math.Sign() method, and also learnt how to use this method with the help of C# example programs.