Decimal.Compare(Decimal, Decimal) Metoda
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Porównuje dwie określone Decimal wartości.
public:
static int Compare(System::Decimal d1, System::Decimal d2);
public static int Compare (decimal d1, decimal d2);
static member Compare : decimal * decimal -> int
Public Shared Function Compare (d1 As Decimal, d2 As Decimal) As Integer
- d1
- Decimal
Pierwsza wartość do porównania.
- d2
- Decimal
Druga wartość do porównania.
Liczba ze podpisem wskazująca wartości względne d1
i d2
.
Wartość zwracana | Znaczenie |
---|---|
Mniej niż zero | d1 jest mniejsze niż d2 .
|
Zero | d1 Wartości d2 i są równe.
|
Większe od zera | d1 wartość jest większa niż d2 .
|
Poniższy przykład porównuje kilka Decimal wartości. Pierwsze porównanie wskazuje, że dwie wartości są równe pomimo operacji odejmowania wykonywanej na value2
zmiennej. Wynika to z tego, że typ ma 29 cyfr dokładności, natomiast różnicę między tymi dwiema wartościami można wykryć tylko z Decimal 30 cyframi dokładności.
using System;
public enum Relationship
{ LessThan = -1, Equals = 0, GreaterThan = 1 }
public class Example
{
public static void Main()
{
decimal value1 = Decimal.MaxValue;
decimal value2 = value1 - .01m;
Console.WriteLine("{0} {2} {1}", value1, value2,
(Relationship) Decimal.Compare(value1, value2));
value2 = value1 / 12m - .1m;
value1 = value1 / 12m;
Console.WriteLine("{0} {2} {1}", value1, value2,
(Relationship) Decimal.Compare(value1, value2));
value1 = value1 - .2m;
value2 = value2 + .1m;
Console.WriteLine("{0} {2} {1}", value1, value2,
(Relationship) Decimal.Compare(value1, value2));
}
}
// The example displays the following output:
// 79228162514264337593543950335 Equals 79228162514264337593543950335
// 6602346876188694799461995861.2 GreaterThan 6602346876188694799461995861.1
// 6602346876188694799461995861.0 LessThan 6602346876188694799461995861.2
Public Enum Relationship As Integer
LessThan = -1
Equals = 0
GreaterThan = 1
End Enum
Module Example
Public Sub Main()
Dim value1 As Decimal = Decimal.MaxValue
Dim value2 As Decimal = value1 - .01d
Console.WriteLine("{0} {2} {1}", value1, value2,
CType(Decimal.Compare(value1, value2), Relationship))
value2 = value1 / 12d - .1d
value1 = value1 / 12d
Console.WriteLine("{0} {2} {1}", value1, value2,
CType(Decimal.Compare(value1, value2), Relationship))
value1 = value1 - .2d
value2 = value2 + .1d
Console.WriteLine("{0} {2} {1}", value1, value2,
CType(Decimal.Compare(value1, value2), Relationship))
End Sub
End Module
' The example displays the following output:
' 79228162514264337593543950335 Equals 79228162514264337593543950335
' 6602346876188694799461995861.2 GreaterThan 6602346876188694799461995861.1
' 6602346876188694799461995861.0 LessThan 6602346876188694799461995861.2
Produkt | Wersje |
---|---|
.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 |
.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 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |