Exportar (0) Imprimir
Expandir Tudo
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original

Método Math.Sign (Int32)

Retorna um valor indicando o sinal de um inteiro com sinal com sinal de 32 bit.

Namespace:  System
Assembly:  mscorlib (em mscorlib.dll)

public static int Sign(
	int value
)

Parâmetros

value
Tipo: System.Int32
Um número assinado.

Valor de retorno

Tipo: System.Int32
Um número indicando o sinal de value.

Número

Descrição

-1

value é menor que zero.

0

value é igual a zero.

1

value é maior que zero.

O exemplo a seguir demonstra como usar o método de entrada para determinar o sinal de um valor Int32 e imprimi-lo usando o console.

// This example demonstrates Math.Sign()using System;

class Sample 
{
    publicstaticvoid Main() 
    {
    string str = "{0}: {1,3} is {2} zero.";
    string nl = Environment.NewLine;

    byte     xByte1    = 0;	
    short    xShort1   = -2;
    int      xInt1     = -3;
    long     xLong1    = -4;
    float    xSingle1  = 0.0f;
    double   xDouble1  = 6.0;
    Decimal  xDecimal1 = -7m;

// The following type is not CLS-compliant.
    sbyte    xSbyte1   = -101;

    Console.WriteLine("{0}Test the sign of the following types of values:", nl);
    Console.WriteLine(str, "Byte   ", xByte1, Test(Math.Sign(xByte1)));
    Console.WriteLine(str, "Int16  ", xShort1, Test(Math.Sign(xShort1)));
    Console.WriteLine(str, "Int32  ", xInt1, Test(Math.Sign(xInt1)));
    Console.WriteLine(str, "Int64  ", xLong1, Test(Math.Sign(xLong1)));
    Console.WriteLine(str, "Single ", xSingle1, Test(Math.Sign(xSingle1)));
    Console.WriteLine(str, "Double ", xDouble1, Test(Math.Sign(xDouble1)));
    Console.WriteLine(str, "Decimal", xDecimal1, Test(Math.Sign(xDecimal1)));
//
    Console.WriteLine("{0}The following type is not CLS-compliant.", nl);
    Console.WriteLine(str, "SByte  ", xSbyte1, Test(Math.Sign(xSbyte1)));
    }
//publicstatic String Test(int compare)
    {
    if (compare == 0) 
       return"equal to";
    elseif (compare < 0)  
        return"less than";
    elsereturn"greater than";
    }
}
/*
This example produces the following results:

Test the sign of the following types of values:
Byte   :   0 is equal to zero.
Int16  :  -2 is less than zero.
Int32  :  -3 is less than zero.
Int64  :  -4 is less than zero.
Single :   0 is equal to zero.
Double :   6 is greater than zero.
Decimal:  -7 is less than zero.

The following type is not CLS-compliant.
SByte  : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        String str = "{0}: {1,3} is {2} zero.";
        String nl = Environment.get_NewLine();

        ubyte xByte1 = 0;
        short xShort1 = -2;
        int xInt1 = -3;
        long xLong1 = -4;
        float xSingle1 = 0;
        double xDouble1 = 6.0;
        Decimal xDecimal1 = new Decimal(-7);

        // The following type is not CLS-compliant.
        byte xSbyte1 = -101;

        Console.WriteLine("{0}Test the sign of the following types of values:",
            nl);
        Console.WriteLine(str, "Byte   ", System.Convert.ToString(xByte1), 
            Test(System.Math.Sign(xByte1)));
        Console.WriteLine(str, "Int16  ", System.Convert.ToString(xShort1), 
            Test(System.Math.Sign(xShort1)));
        Console.WriteLine(str, "Int32  ", System.Convert.ToString(xInt1), 
            Test(System.Math.Sign(xInt1)));
        Console.WriteLine(str, "Int64  ", System.Convert.ToString(xLong1), 
            Test(System.Math.Sign(xLong1)));
        Console.WriteLine(str, "Single ", System.Convert.ToString(xSingle1), 
            Test(System.Math.Sign(xSingle1)));
        Console.WriteLine(str, "Double ", System.Convert.ToString(xDouble1), 
            Test(System.Math.Sign(xDouble1)));
        Console.WriteLine(str, "Decimal", System.Convert.ToString(xDecimal1), 
            Test(System.Math.Sign(xDecimal1)));
        //
        Console.WriteLine("{0}The following type is not CLS-compliant.", nl);
        Console.WriteLine(str, "SByte  ", System.Convert.ToString(xSbyte1), 
            Test(System.Math.Sign(xSbyte1)));
    } //main

    //
    public static String Test(int compare)
    {
        if (compare == 0) {
            return "equal to";
        }
        else {
            if (compare < 0) {
                return "less than";
            }
            else {
                return "greater than";
            }
        }
    } //Test 

/*
This example produces the following results:

Test the sign of the following types of values:
Byte   :   0 is equal to zero.
Int16  :  -2 is less than zero.
Int32  :  -3 is less than zero.
Int64  :  -4 is less than zero.
Single :   0 is equal to zero.
Double :   6 is greater than zero.
Decimal:  -7 is less than zero.

The following type is not CLS-compliant.
SByte  : -101 is less than zero.
*/
} //Sample


Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.

.NET Framework

Compatível com: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatível com: 3.5, 2.0, 1.0

XNA Framework

Compatível com: , 1.0

Contribuições da comunidade

ADICIONAR
Mostrar:
© 2014 Microsoft