Math.Sign Метод
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Возвращает целое число, указывающее знак числа.
Sign(IntPtr) |
Возвращает целое число, указывающее знак собственного целого числа со знаком. |
Sign(Single) |
Возвращает целое число, указывающее знак числа с плавающей запятой с одной точностью. |
Sign(SByte) |
Возвращает целое число, указывающее знак 8-разрядного целого числа со знаком. |
Sign(Int64) |
Возвращает целое число, указывающее знак 64-разрядного целого числа со знаком. |
Sign(Double) |
Возвращает целое число, указывающее знак числа с плавающей запятой двойной точности. |
Sign(Int16) |
Возвращает целое число, указывающее знак 16-разрядного целого числа со знаком. |
Sign(Decimal) |
Возвращает целое число, указывающее знак десятичного числа. |
Sign(Int32) |
Возвращает целое число, указывающее знак 32-разрядного целого числа со знаком. |
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
Возвращает целое число, указывающее знак собственного целого числа со знаком.
public:
static int Sign(IntPtr value);
public static int Sign (nint value);
public static int Sign (IntPtr value);
static member Sign : nativeint -> int
Public Shared Function Sign (value As IntPtr) As Integer
Параметры
- value
-
IntPtr
nint
nativeint
Подписанный номер.
Возвращаемое значение
Число, указывающее знак value
, как показано в следующей таблице.
Возвращаемое значение | Значение |
---|---|
-1 |
value меньше нуля.
|
0 |
value равно нулю.
|
1 |
value больше нуля.
|
Примеры
В следующем примере показано, как использовать метод Sign(IntPtr) для определения знака значения IntPtr и отображения его в консоли.
// This example demonstrates Math.Sign()
using System;
class Sample
{
public static void 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;
nint xIntPtr1 = 8;
// The following type is not CLS-compliant.
sbyte xSbyte1 = -101;
Console.WriteLine($"{nl}Test the sign of the following types of values:");
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(str, "IntPtr", xIntPtr1, Test(Math.Sign(xIntPtr1)));
Console.WriteLine($"{nl}The following type is not CLS-compliant.");
Console.WriteLine(str, "SByte ", xSbyte1, Test(Math.Sign(xSbyte1)));
}
public static string Test(int compare)
{
if (compare == 0)
return "equal to";
else if (compare < 0)
return "less than";
else
return "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.
IntPtr: 8 is greater than zero.
The following type is not CLS-compliant.
SByte : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
// In F#, the sign function may be used instead
open System
let test = function
| 0 ->
"equal to"
| x when x < 0 ->
"less than"
| _ ->
"greater than"
let print typ a b =
printfn $"{typ}: {a,3} is {b} zero."
let xByte1 = 0uy
let xShort1 = -2s
let xInt1 = -3
let xLong1 = -4L
let xSingle1 = 0f
let xDouble1 = 6.
let xDecimal1 = -7m
let xIntPtr1 = 8
// The following type is not CLS-compliant.
let xSbyte1 = -101y
printfn "\nTest the sign of the following types of values:"
print "Byte " xByte1 (test (Math.Sign xByte1))
print "Int16 " xShort1 (test (Math.Sign xShort1))
print "Int32 " xInt1 (test (Math.Sign xInt1))
print "Int64 " xLong1 (test (Math.Sign xLong1))
print "Single " xSingle1 (test (Math.Sign xSingle1))
print "Double " xDouble1 (test (Math.Sign xDouble1))
print "Decimal" xDecimal1 (test (Math.Sign xDecimal1))
print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1))
printfn "\nThe following type is not CLS-compliant."
print "SByte " xSbyte1 (test (Math.Sign xSbyte1))
// 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.
// IntPtr: 8 is greater than zero.
//
// The following type is not CLS-compliant.
// SByte : -101 is less than zero.
Применяется к
.NET 9 және басқа нұсқалар
Өнім | Нұсқалар |
---|---|
.NET | 6, 7, 8, 9 |
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
Возвращает целое число, указывающее знак числа с плавающей запятой с одной точностью.
public:
static int Sign(float value);
public static int Sign (float value);
static member Sign : single -> int
Public Shared Function Sign (value As Single) As Integer
Параметры
- value
- Single
Подписанный номер.
Возвращаемое значение
Число, указывающее знак value
, как показано в следующей таблице.
Возвращаемое значение | Значение |
---|---|
-1 |
value меньше нуля.
|
0 |
value равно нулю.
|
1 |
value больше нуля.
|
Исключения
value
равно NaN.
Примеры
В следующем примере показано, как использовать метод Sign(Single) для определения знака значения Single и отображения его в консоли.
// This example demonstrates Math.Sign()
using namespace System;
String^ Test( int compare )
{
if ( compare == 0 )
return "equal to";
else
if ( compare < 0 )
return "less than";
else
return "greater than";
}
int 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 = -7;
// 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 ) ) );
}
/*
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()
using System;
class Sample
{
public static void 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;
nint xIntPtr1 = 8;
// The following type is not CLS-compliant.
sbyte xSbyte1 = -101;
Console.WriteLine($"{nl}Test the sign of the following types of values:");
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(str, "IntPtr", xIntPtr1, Test(Math.Sign(xIntPtr1)));
Console.WriteLine($"{nl}The following type is not CLS-compliant.");
Console.WriteLine(str, "SByte ", xSbyte1, Test(Math.Sign(xSbyte1)));
}
public static string Test(int compare)
{
if (compare == 0)
return "equal to";
else if (compare < 0)
return "less than";
else
return "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.
IntPtr: 8 is greater than zero.
The following type is not CLS-compliant.
SByte : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
// In F#, the sign function may be used instead
open System
let test = function
| 0 ->
"equal to"
| x when x < 0 ->
"less than"
| _ ->
"greater than"
let print typ a b =
printfn $"{typ}: {a,3} is {b} zero."
let xByte1 = 0uy
let xShort1 = -2s
let xInt1 = -3
let xLong1 = -4L
let xSingle1 = 0f
let xDouble1 = 6.
let xDecimal1 = -7m
let xIntPtr1 = 8
// The following type is not CLS-compliant.
let xSbyte1 = -101y
printfn "\nTest the sign of the following types of values:"
print "Byte " xByte1 (test (Math.Sign xByte1))
print "Int16 " xShort1 (test (Math.Sign xShort1))
print "Int32 " xInt1 (test (Math.Sign xInt1))
print "Int64 " xLong1 (test (Math.Sign xLong1))
print "Single " xSingle1 (test (Math.Sign xSingle1))
print "Double " xDouble1 (test (Math.Sign xDouble1))
print "Decimal" xDecimal1 (test (Math.Sign xDecimal1))
print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1))
printfn "\nThe following type is not CLS-compliant."
print "SByte " xSbyte1 (test (Math.Sign xSbyte1))
// 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.
// IntPtr: 8 is greater than zero.
//
// The following type is not CLS-compliant.
// SByte : -101 is less than zero.
' This example demonstrates Math.Sign()
Class Sample
Public Shared Sub Main()
Dim str As String = "{0}: {1,3} is {2} zero."
Dim nl As String = Environment.NewLine
Dim xByte1 As Byte = 0
Dim xShort1 As Short = -2
Dim xInt1 As Integer = -3
Dim xLong1 As Long = -4
Dim xSingle1 As Single = 0F
Dim xDouble1 As Double = 6.0
Dim xDecimal1 As [Decimal] = -7D
' The following type is not CLS-compliant.
Dim xSbyte1 As SByte = -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)))
End Sub
'
Public Shared Function Test([compare] As Integer) As [String]
If [compare] = 0 Then
Return "equal to"
ElseIf [compare] < 0 Then
Return "less than"
Else
Return "greater than"
End If
End Function 'Test
End Class
'
'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.
Применяется к
.NET 9 және басқа нұсқалар
Өнім | Нұсқалар |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
Важно!
Этот API несовместим с CLS.
Возвращает целое число, указывающее знак 8-разрядного целого числа со знаком.
public:
static int Sign(System::SByte value);
[System.CLSCompliant(false)]
public static int Sign (sbyte value);
[<System.CLSCompliant(false)>]
static member Sign : sbyte -> int
Public Shared Function Sign (value As SByte) As Integer
Параметры
- value
- SByte
Подписанный номер.
Возвращаемое значение
Число, указывающее знак value
, как показано в следующей таблице.
Возвращаемое значение | Значение |
---|---|
-1 |
value меньше нуля.
|
0 |
value равно нулю.
|
1 |
value больше нуля.
|
- Атрибуты
Примеры
В следующем примере показано, как использовать метод Sign(SByte) для определения знака значения SByte и отображения его в консоли.
// This example demonstrates Math.Sign()
using namespace System;
String^ Test( int compare )
{
if ( compare == 0 )
return "equal to";
else
if ( compare < 0 )
return "less than";
else
return "greater than";
}
int 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 = -7;
// 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 ) ) );
}
/*
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()
using System;
class Sample
{
public static void 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;
nint xIntPtr1 = 8;
// The following type is not CLS-compliant.
sbyte xSbyte1 = -101;
Console.WriteLine($"{nl}Test the sign of the following types of values:");
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(str, "IntPtr", xIntPtr1, Test(Math.Sign(xIntPtr1)));
Console.WriteLine($"{nl}The following type is not CLS-compliant.");
Console.WriteLine(str, "SByte ", xSbyte1, Test(Math.Sign(xSbyte1)));
}
public static string Test(int compare)
{
if (compare == 0)
return "equal to";
else if (compare < 0)
return "less than";
else
return "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.
IntPtr: 8 is greater than zero.
The following type is not CLS-compliant.
SByte : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
// In F#, the sign function may be used instead
open System
let test = function
| 0 ->
"equal to"
| x when x < 0 ->
"less than"
| _ ->
"greater than"
let print typ a b =
printfn $"{typ}: {a,3} is {b} zero."
let xByte1 = 0uy
let xShort1 = -2s
let xInt1 = -3
let xLong1 = -4L
let xSingle1 = 0f
let xDouble1 = 6.
let xDecimal1 = -7m
let xIntPtr1 = 8
// The following type is not CLS-compliant.
let xSbyte1 = -101y
printfn "\nTest the sign of the following types of values:"
print "Byte " xByte1 (test (Math.Sign xByte1))
print "Int16 " xShort1 (test (Math.Sign xShort1))
print "Int32 " xInt1 (test (Math.Sign xInt1))
print "Int64 " xLong1 (test (Math.Sign xLong1))
print "Single " xSingle1 (test (Math.Sign xSingle1))
print "Double " xDouble1 (test (Math.Sign xDouble1))
print "Decimal" xDecimal1 (test (Math.Sign xDecimal1))
print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1))
printfn "\nThe following type is not CLS-compliant."
print "SByte " xSbyte1 (test (Math.Sign xSbyte1))
// 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.
// IntPtr: 8 is greater than zero.
//
// The following type is not CLS-compliant.
// SByte : -101 is less than zero.
' This example demonstrates Math.Sign()
Class Sample
Public Shared Sub Main()
Dim str As String = "{0}: {1,3} is {2} zero."
Dim nl As String = Environment.NewLine
Dim xByte1 As Byte = 0
Dim xShort1 As Short = -2
Dim xInt1 As Integer = -3
Dim xLong1 As Long = -4
Dim xSingle1 As Single = 0F
Dim xDouble1 As Double = 6.0
Dim xDecimal1 As [Decimal] = -7D
' The following type is not CLS-compliant.
Dim xSbyte1 As SByte = -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)))
End Sub
'
Public Shared Function Test([compare] As Integer) As [String]
If [compare] = 0 Then
Return "equal to"
ElseIf [compare] < 0 Then
Return "less than"
Else
Return "greater than"
End If
End Function 'Test
End Class
'
'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.
Применяется к
.NET 9 және басқа нұсқалар
Өнім | Нұсқалар |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
Возвращает целое число, указывающее знак 64-разрядного целого числа со знаком.
public:
static int Sign(long value);
public static int Sign (long value);
static member Sign : int64 -> int
Public Shared Function Sign (value As Long) As Integer
Параметры
- value
- Int64
Подписанный номер.
Возвращаемое значение
Число, указывающее знак value
, как показано в следующей таблице.
Возвращаемое значение | Значение |
---|---|
-1 |
value меньше нуля.
|
0 |
value равно нулю.
|
1 |
value больше нуля.
|
Примеры
В следующем примере показано, как использовать метод Sign(Int64) для определения знака значения Int64 и отображения его в консоли.
// This example demonstrates Math.Sign()
using namespace System;
String^ Test( int compare )
{
if ( compare == 0 )
return "equal to";
else
if ( compare < 0 )
return "less than";
else
return "greater than";
}
int 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 = -7;
// 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 ) ) );
}
/*
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()
using System;
class Sample
{
public static void 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;
nint xIntPtr1 = 8;
// The following type is not CLS-compliant.
sbyte xSbyte1 = -101;
Console.WriteLine($"{nl}Test the sign of the following types of values:");
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(str, "IntPtr", xIntPtr1, Test(Math.Sign(xIntPtr1)));
Console.WriteLine($"{nl}The following type is not CLS-compliant.");
Console.WriteLine(str, "SByte ", xSbyte1, Test(Math.Sign(xSbyte1)));
}
public static string Test(int compare)
{
if (compare == 0)
return "equal to";
else if (compare < 0)
return "less than";
else
return "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.
IntPtr: 8 is greater than zero.
The following type is not CLS-compliant.
SByte : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
// In F#, the sign function may be used instead
open System
let test = function
| 0 ->
"equal to"
| x when x < 0 ->
"less than"
| _ ->
"greater than"
let print typ a b =
printfn $"{typ}: {a,3} is {b} zero."
let xByte1 = 0uy
let xShort1 = -2s
let xInt1 = -3
let xLong1 = -4L
let xSingle1 = 0f
let xDouble1 = 6.
let xDecimal1 = -7m
let xIntPtr1 = 8
// The following type is not CLS-compliant.
let xSbyte1 = -101y
printfn "\nTest the sign of the following types of values:"
print "Byte " xByte1 (test (Math.Sign xByte1))
print "Int16 " xShort1 (test (Math.Sign xShort1))
print "Int32 " xInt1 (test (Math.Sign xInt1))
print "Int64 " xLong1 (test (Math.Sign xLong1))
print "Single " xSingle1 (test (Math.Sign xSingle1))
print "Double " xDouble1 (test (Math.Sign xDouble1))
print "Decimal" xDecimal1 (test (Math.Sign xDecimal1))
print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1))
printfn "\nThe following type is not CLS-compliant."
print "SByte " xSbyte1 (test (Math.Sign xSbyte1))
// 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.
// IntPtr: 8 is greater than zero.
//
// The following type is not CLS-compliant.
// SByte : -101 is less than zero.
' This example demonstrates Math.Sign()
Class Sample
Public Shared Sub Main()
Dim str As String = "{0}: {1,3} is {2} zero."
Dim nl As String = Environment.NewLine
Dim xByte1 As Byte = 0
Dim xShort1 As Short = -2
Dim xInt1 As Integer = -3
Dim xLong1 As Long = -4
Dim xSingle1 As Single = 0F
Dim xDouble1 As Double = 6.0
Dim xDecimal1 As [Decimal] = -7D
' The following type is not CLS-compliant.
Dim xSbyte1 As SByte = -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)))
End Sub
'
Public Shared Function Test([compare] As Integer) As [String]
If [compare] = 0 Then
Return "equal to"
ElseIf [compare] < 0 Then
Return "less than"
Else
Return "greater than"
End If
End Function 'Test
End Class
'
'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.
Применяется к
.NET 9 және басқа нұсқалар
Өнім | Нұсқалар |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
Возвращает целое число, указывающее знак числа с плавающей запятой двойной точности.
public:
static int Sign(double value);
public static int Sign (double value);
static member Sign : double -> int
Public Shared Function Sign (value As Double) As Integer
Параметры
- value
- Double
Подписанный номер.
Возвращаемое значение
Число, указывающее знак value
, как показано в следующей таблице.
Возвращаемое значение | Значение |
---|---|
-1 |
value меньше нуля.
|
0 |
value равно нулю.
|
1 |
value больше нуля.
|
Исключения
value
равно NaN.
Примеры
В следующем примере показано, как использовать метод Sign(Double) для определения знака значения Double и отображения его в консоли.
// This example demonstrates Math.Sign()
using namespace System;
String^ Test( int compare )
{
if ( compare == 0 )
return "equal to";
else
if ( compare < 0 )
return "less than";
else
return "greater than";
}
int 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 = -7;
// 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 ) ) );
}
/*
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()
using System;
class Sample
{
public static void 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;
nint xIntPtr1 = 8;
// The following type is not CLS-compliant.
sbyte xSbyte1 = -101;
Console.WriteLine($"{nl}Test the sign of the following types of values:");
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(str, "IntPtr", xIntPtr1, Test(Math.Sign(xIntPtr1)));
Console.WriteLine($"{nl}The following type is not CLS-compliant.");
Console.WriteLine(str, "SByte ", xSbyte1, Test(Math.Sign(xSbyte1)));
}
public static string Test(int compare)
{
if (compare == 0)
return "equal to";
else if (compare < 0)
return "less than";
else
return "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.
IntPtr: 8 is greater than zero.
The following type is not CLS-compliant.
SByte : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
// In F#, the sign function may be used instead
open System
let test = function
| 0 ->
"equal to"
| x when x < 0 ->
"less than"
| _ ->
"greater than"
let print typ a b =
printfn $"{typ}: {a,3} is {b} zero."
let xByte1 = 0uy
let xShort1 = -2s
let xInt1 = -3
let xLong1 = -4L
let xSingle1 = 0f
let xDouble1 = 6.
let xDecimal1 = -7m
let xIntPtr1 = 8
// The following type is not CLS-compliant.
let xSbyte1 = -101y
printfn "\nTest the sign of the following types of values:"
print "Byte " xByte1 (test (Math.Sign xByte1))
print "Int16 " xShort1 (test (Math.Sign xShort1))
print "Int32 " xInt1 (test (Math.Sign xInt1))
print "Int64 " xLong1 (test (Math.Sign xLong1))
print "Single " xSingle1 (test (Math.Sign xSingle1))
print "Double " xDouble1 (test (Math.Sign xDouble1))
print "Decimal" xDecimal1 (test (Math.Sign xDecimal1))
print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1))
printfn "\nThe following type is not CLS-compliant."
print "SByte " xSbyte1 (test (Math.Sign xSbyte1))
// 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.
// IntPtr: 8 is greater than zero.
//
// The following type is not CLS-compliant.
// SByte : -101 is less than zero.
' This example demonstrates Math.Sign()
Class Sample
Public Shared Sub Main()
Dim str As String = "{0}: {1,3} is {2} zero."
Dim nl As String = Environment.NewLine
Dim xByte1 As Byte = 0
Dim xShort1 As Short = -2
Dim xInt1 As Integer = -3
Dim xLong1 As Long = -4
Dim xSingle1 As Single = 0F
Dim xDouble1 As Double = 6.0
Dim xDecimal1 As [Decimal] = -7D
' The following type is not CLS-compliant.
Dim xSbyte1 As SByte = -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)))
End Sub
'
Public Shared Function Test([compare] As Integer) As [String]
If [compare] = 0 Then
Return "equal to"
ElseIf [compare] < 0 Then
Return "less than"
Else
Return "greater than"
End If
End Function 'Test
End Class
'
'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.
Применяется к
.NET 9 және басқа нұсқалар
Өнім | Нұсқалар |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
Возвращает целое число, указывающее знак 16-разрядного целого числа со знаком.
public:
static int Sign(short value);
public static int Sign (short value);
static member Sign : int16 -> int
Public Shared Function Sign (value As Short) As Integer
Параметры
- value
- Int16
Подписанный номер.
Возвращаемое значение
Число, указывающее знак value
, как показано в следующей таблице.
Возвращаемое значение | Значение |
---|---|
-1 |
value меньше нуля.
|
0 |
value равно нулю.
|
1 |
value больше нуля.
|
Примеры
В следующем примере показано, как использовать метод Sign(Int16) для определения знака значения Int16 и отображения его в консоли.
// This example demonstrates Math.Sign()
using namespace System;
String^ Test( int compare )
{
if ( compare == 0 )
return "equal to";
else
if ( compare < 0 )
return "less than";
else
return "greater than";
}
int 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 = -7;
// 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 ) ) );
}
/*
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()
using System;
class Sample
{
public static void 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;
nint xIntPtr1 = 8;
// The following type is not CLS-compliant.
sbyte xSbyte1 = -101;
Console.WriteLine($"{nl}Test the sign of the following types of values:");
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(str, "IntPtr", xIntPtr1, Test(Math.Sign(xIntPtr1)));
Console.WriteLine($"{nl}The following type is not CLS-compliant.");
Console.WriteLine(str, "SByte ", xSbyte1, Test(Math.Sign(xSbyte1)));
}
public static string Test(int compare)
{
if (compare == 0)
return "equal to";
else if (compare < 0)
return "less than";
else
return "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.
IntPtr: 8 is greater than zero.
The following type is not CLS-compliant.
SByte : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
// In F#, the sign function may be used instead
open System
let test = function
| 0 ->
"equal to"
| x when x < 0 ->
"less than"
| _ ->
"greater than"
let print typ a b =
printfn $"{typ}: {a,3} is {b} zero."
let xByte1 = 0uy
let xShort1 = -2s
let xInt1 = -3
let xLong1 = -4L
let xSingle1 = 0f
let xDouble1 = 6.
let xDecimal1 = -7m
let xIntPtr1 = 8
// The following type is not CLS-compliant.
let xSbyte1 = -101y
printfn "\nTest the sign of the following types of values:"
print "Byte " xByte1 (test (Math.Sign xByte1))
print "Int16 " xShort1 (test (Math.Sign xShort1))
print "Int32 " xInt1 (test (Math.Sign xInt1))
print "Int64 " xLong1 (test (Math.Sign xLong1))
print "Single " xSingle1 (test (Math.Sign xSingle1))
print "Double " xDouble1 (test (Math.Sign xDouble1))
print "Decimal" xDecimal1 (test (Math.Sign xDecimal1))
print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1))
printfn "\nThe following type is not CLS-compliant."
print "SByte " xSbyte1 (test (Math.Sign xSbyte1))
// 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.
// IntPtr: 8 is greater than zero.
//
// The following type is not CLS-compliant.
// SByte : -101 is less than zero.
' This example demonstrates Math.Sign()
Class Sample
Public Shared Sub Main()
Dim str As String = "{0}: {1,3} is {2} zero."
Dim nl As String = Environment.NewLine
Dim xByte1 As Byte = 0
Dim xShort1 As Short = -2
Dim xInt1 As Integer = -3
Dim xLong1 As Long = -4
Dim xSingle1 As Single = 0F
Dim xDouble1 As Double = 6.0
Dim xDecimal1 As [Decimal] = -7D
' The following type is not CLS-compliant.
Dim xSbyte1 As SByte = -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)))
End Sub
'
Public Shared Function Test([compare] As Integer) As [String]
If [compare] = 0 Then
Return "equal to"
ElseIf [compare] < 0 Then
Return "less than"
Else
Return "greater than"
End If
End Function 'Test
End Class
'
'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.
Применяется к
.NET 9 және басқа нұсқалар
Өнім | Нұсқалар |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
Возвращает целое число, указывающее знак десятичного числа.
public:
static int Sign(System::Decimal value);
public static int Sign (decimal value);
static member Sign : decimal -> int
Public Shared Function Sign (value As Decimal) As Integer
Параметры
- value
- Decimal
Подписываемое десятичное число.
Возвращаемое значение
Число, указывающее знак value
, как показано в следующей таблице.
Возвращаемое значение | Значение |
---|---|
-1 |
value меньше нуля.
|
0 |
value равно нулю.
|
1 |
value больше нуля.
|
Примеры
В следующем примере показано, как использовать метод Sign(Decimal) для определения знака значения Decimal и отображения его в консоли.
// This example demonstrates Math.Sign()
using namespace System;
String^ Test( int compare )
{
if ( compare == 0 )
return "equal to";
else
if ( compare < 0 )
return "less than";
else
return "greater than";
}
int 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 = -7;
// 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 ) ) );
}
/*
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()
using System;
class Sample
{
public static void 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;
nint xIntPtr1 = 8;
// The following type is not CLS-compliant.
sbyte xSbyte1 = -101;
Console.WriteLine($"{nl}Test the sign of the following types of values:");
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(str, "IntPtr", xIntPtr1, Test(Math.Sign(xIntPtr1)));
Console.WriteLine($"{nl}The following type is not CLS-compliant.");
Console.WriteLine(str, "SByte ", xSbyte1, Test(Math.Sign(xSbyte1)));
}
public static string Test(int compare)
{
if (compare == 0)
return "equal to";
else if (compare < 0)
return "less than";
else
return "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.
IntPtr: 8 is greater than zero.
The following type is not CLS-compliant.
SByte : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
// In F#, the sign function may be used instead
open System
let test = function
| 0 ->
"equal to"
| x when x < 0 ->
"less than"
| _ ->
"greater than"
let print typ a b =
printfn $"{typ}: {a,3} is {b} zero."
let xByte1 = 0uy
let xShort1 = -2s
let xInt1 = -3
let xLong1 = -4L
let xSingle1 = 0f
let xDouble1 = 6.
let xDecimal1 = -7m
let xIntPtr1 = 8
// The following type is not CLS-compliant.
let xSbyte1 = -101y
printfn "\nTest the sign of the following types of values:"
print "Byte " xByte1 (test (Math.Sign xByte1))
print "Int16 " xShort1 (test (Math.Sign xShort1))
print "Int32 " xInt1 (test (Math.Sign xInt1))
print "Int64 " xLong1 (test (Math.Sign xLong1))
print "Single " xSingle1 (test (Math.Sign xSingle1))
print "Double " xDouble1 (test (Math.Sign xDouble1))
print "Decimal" xDecimal1 (test (Math.Sign xDecimal1))
print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1))
printfn "\nThe following type is not CLS-compliant."
print "SByte " xSbyte1 (test (Math.Sign xSbyte1))
// 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.
// IntPtr: 8 is greater than zero.
//
// The following type is not CLS-compliant.
// SByte : -101 is less than zero.
' This example demonstrates Math.Sign()
Class Sample
Public Shared Sub Main()
Dim str As String = "{0}: {1,3} is {2} zero."
Dim nl As String = Environment.NewLine
Dim xByte1 As Byte = 0
Dim xShort1 As Short = -2
Dim xInt1 As Integer = -3
Dim xLong1 As Long = -4
Dim xSingle1 As Single = 0F
Dim xDouble1 As Double = 6.0
Dim xDecimal1 As [Decimal] = -7D
' The following type is not CLS-compliant.
Dim xSbyte1 As SByte = -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)))
End Sub
'
Public Shared Function Test([compare] As Integer) As [String]
If [compare] = 0 Then
Return "equal to"
ElseIf [compare] < 0 Then
Return "less than"
Else
Return "greater than"
End If
End Function 'Test
End Class
'
'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.
Применяется к
.NET 9 және басқа нұсқалар
Өнім | Нұсқалар |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
- Исходный код:
- Math.cs
Возвращает целое число, указывающее знак 32-разрядного целого числа со знаком.
public:
static int Sign(int value);
public static int Sign (int value);
static member Sign : int -> int
Public Shared Function Sign (value As Integer) As Integer
Параметры
- value
- Int32
Подписанный номер.
Возвращаемое значение
Число, указывающее знак value
, как показано в следующей таблице.
Возвращаемое значение | Значение |
---|---|
-1 |
value меньше нуля.
|
0 |
value равно нулю.
|
1 |
value больше нуля.
|
Примеры
В следующем примере показано, как использовать метод Sign(Int32) для определения знака значения Int32 и отображения его в консоли.
// This example demonstrates Math.Sign()
using namespace System;
String^ Test( int compare )
{
if ( compare == 0 )
return "equal to";
else
if ( compare < 0 )
return "less than";
else
return "greater than";
}
int 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 = -7;
// 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 ) ) );
}
/*
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()
using System;
class Sample
{
public static void 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;
nint xIntPtr1 = 8;
// The following type is not CLS-compliant.
sbyte xSbyte1 = -101;
Console.WriteLine($"{nl}Test the sign of the following types of values:");
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(str, "IntPtr", xIntPtr1, Test(Math.Sign(xIntPtr1)));
Console.WriteLine($"{nl}The following type is not CLS-compliant.");
Console.WriteLine(str, "SByte ", xSbyte1, Test(Math.Sign(xSbyte1)));
}
public static string Test(int compare)
{
if (compare == 0)
return "equal to";
else if (compare < 0)
return "less than";
else
return "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.
IntPtr: 8 is greater than zero.
The following type is not CLS-compliant.
SByte : -101 is less than zero.
*/
// This example demonstrates Math.Sign()
// In F#, the sign function may be used instead
open System
let test = function
| 0 ->
"equal to"
| x when x < 0 ->
"less than"
| _ ->
"greater than"
let print typ a b =
printfn $"{typ}: {a,3} is {b} zero."
let xByte1 = 0uy
let xShort1 = -2s
let xInt1 = -3
let xLong1 = -4L
let xSingle1 = 0f
let xDouble1 = 6.
let xDecimal1 = -7m
let xIntPtr1 = 8
// The following type is not CLS-compliant.
let xSbyte1 = -101y
printfn "\nTest the sign of the following types of values:"
print "Byte " xByte1 (test (Math.Sign xByte1))
print "Int16 " xShort1 (test (Math.Sign xShort1))
print "Int32 " xInt1 (test (Math.Sign xInt1))
print "Int64 " xLong1 (test (Math.Sign xLong1))
print "Single " xSingle1 (test (Math.Sign xSingle1))
print "Double " xDouble1 (test (Math.Sign xDouble1))
print "Decimal" xDecimal1 (test (Math.Sign xDecimal1))
print "IntPtr" xIntPtr1 (test (Math.Sign xIntPtr1))
printfn "\nThe following type is not CLS-compliant."
print "SByte " xSbyte1 (test (Math.Sign xSbyte1))
// 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.
// IntPtr: 8 is greater than zero.
//
// The following type is not CLS-compliant.
// SByte : -101 is less than zero.
' This example demonstrates Math.Sign()
Class Sample
Public Shared Sub Main()
Dim str As String = "{0}: {1,3} is {2} zero."
Dim nl As String = Environment.NewLine
Dim xByte1 As Byte = 0
Dim xShort1 As Short = -2
Dim xInt1 As Integer = -3
Dim xLong1 As Long = -4
Dim xSingle1 As Single = 0F
Dim xDouble1 As Double = 6.0
Dim xDecimal1 As [Decimal] = -7D
' The following type is not CLS-compliant.
Dim xSbyte1 As SByte = -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)))
End Sub
'
Public Shared Function Test([compare] As Integer) As [String]
If [compare] = 0 Then
Return "equal to"
ElseIf [compare] < 0 Then
Return "less than"
Else
Return "greater than"
End If
End Function 'Test
End Class
'
'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.
Применяется к
.NET 9 және басқа нұсқалар
Өнім | Нұсқалар |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET кері байланысы
.NET — бастапқы коды ашық жоба. Пікір қалдыру үшін сілтемені таңдаңыз: