C MathSign 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.

SignDecimal

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

Syntax

The syntax of Sign() method is

Math.Sign(Decimal value)

where

Parameter Description
value A signed decimal number.

Return Value

The method returns Integer value.

Example 1 SignDecimal

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

SignDouble

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

Parameter Description
value A signed double-precision floating-point number.

Return Value

The method returns Integer value.

Example 2 SignDouble

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

SignInt16

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

Parameter Description
value A 16-bit signed integer.

Return Value

The method returns Integer value.

Example 3 SignInt16

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

SignInt32

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

Parameter Description
value A 32-bit signed integer.

Return Value

The method returns Integer value.

Example 4 SignInt32

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

SignInt64

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

Parameter Description
value A 64-bit signed integer.

Return Value

The method returns Integer value.

Example 5 SignInt64

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

SignSByte

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

Parameter Description
SByte An 8-bit signed integer

Return Value

The method returns Integer value.

Example 6 SignSByte

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

SignSingle

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

Parameter Description
value A single-precision floating-point number

Return Value

The method returns Integer value.

Example 7 SignSingle

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.