共用方式為


Decimal.Compare(Decimal, Decimal) 方法

定義

比較兩個指定的 Decimal 值。

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

要比較的第一個值。

d2
Decimal

要比較的第二個值。

傳回

帶正負號的數字,該數字表示 d1d2 的相對值。

傳回值 意義
小於零 d1 小於 d2
d1d2 相等。
大於零 d1 大於 d2

範例

下列範例會比較數個 Decimal 值。 請注意,第一個比較指出,儘管在變數上 value2 執行的減法運算,兩個值都相等。 這是因為 Decimal 類型有 29 位數的有效位數,而這兩個值之間的差異只能偵測到 30 位數的有效位數。

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
open System

type Relationship =
    | LessThan = -1
    | Equals = 0
    | GreaterThan = 1

[<EntryPoint>]
let main _ =
    let value1 = Decimal.MaxValue
    let value2 = value1 - 0.01m
    printfn $"{value1} {Decimal.Compare(value1, value2) |> enum<Relationship>} {value2}"

    let value2 = value1 / 12m - 0.1m
    let value1 = value1 / 12m
    printfn $"{value1} {Decimal.Compare(value1, value2) |> enum<Relationship>} {value2}"

    let value1 = value1 - 0.2m
    let value2 = value2 + 0.1m
    printfn $"{value1} {Decimal.Compare(value1, value2) |> enum<Relationship>} {value2}"

    0

// 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

適用於

另請參閱