Decimal.Compare(Decimal, Decimal) メソッド

定義

2 つの指定した Decimal 値を比較します。Compares two specified Decimal values.

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

比較する最初の値です。The first value to compare.

d2
Decimal

比較する 2 番目の値です。The second value to compare.

戻り値

Int32

d1d2 の相対値を示す符号付き数値。A signed number indicating the relative values of d1 and d2.

戻り値Return value 説明Meaning
0 より小さい値Less than zero d1d2 より小さい値です。d1 is less than d2.
0Zero d1d2 が等しい。d1 and d2 are equal.
0 より大きい値Greater than zero d1d2 より大きくなっています。d1 is greater than d2.

次の例では、複数 Decimal の値を比較します。The following example compares several Decimal values. 最初の比較では、2つの値が等しいかどうかが、変数に対して実行された減算演算に関係していることに注意して value2 ください。Note that the first comparison indicates that the two values are equal despite the subtraction operation performed on the value2 variable. これは、型の有効桁数が29桁であるのに対し、この Decimal 2 つの値の差は30桁の有効桁数でしか検出できないためです。This is because the Decimal type has 29 digits of precision, whereas a difference between these two values can be detected only with 30 digits of precision.

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

適用対象

こちらもご覧ください