Compartir a través de


Decimal.Compare Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Compares two specified Decimal values and returns an integer that indicates whether the first value is greater than, less than, or equal to the second value.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function Compare ( _
    d1 As Decimal, _
    d2 As Decimal _
) As Integer
[SecuritySafeCriticalAttribute]
public static int Compare(
    decimal d1,
    decimal d2
)

Parameters

Return Value

Type: System.Int32
A signed number indicating the relationship between d1 and d2.

Return Value

Meaning

Less than zero

d1 is less than d2.

Zero

d1 and d2 are equal.

Greater than zero

d1 is greater than d2.

Examples

The following code example compares several Decimal values to a reference Decimal value using the Compare method.

' Example of the Decimal.Compare and static Decimal.Equals methods.

Module Example

   Const dataFmt As String = "{0,-45}{1}"

   ' Compare Decimal parameters, and display them with the results.
   Sub CompareDecimals(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Left As Decimal, ByVal Right As Decimal, _
       ByVal RightText As String)

      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format(dataFmt, "Right: " & RightText, Right) & vbCrLf
      outputBlock.Text += String.Format(dataFmt, "Decimal.Equals( Left, Right ) & vbCrLf", _
          Decimal.Equals(Left, Right))
      outputBlock.Text += String.Format(dataFmt, _
          "Decimal.Compare( Left, Right )", _
          Decimal.Compare(Left, Right)) + vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text += _
          "This example of the Decimal.Equals( Decimal, " & _
          "Decimal ) and " & vbCrLf & "Decimal.Compare( " & _
          "Decimal, Decimal ) methods generates the " & vbCrLf & _
          "following output. It creates several different " & _
          "Decimal " & vbCrLf & "values and compares them " & _
          "with the following reference value." & vbCrLf + vbCrLf

      ' Create a reference Decimal value.
      Dim Left As New Decimal(123.456)

      outputBlock.Text += String.Format(dataFmt, "Left: Decimal( 123.456 )", Left) & vbCrLf

      ' Create Decimal values to compare with the reference.
      CompareDecimals(outputBlock, Left, New Decimal(123.456), _
          "Decimal( 1.2345600E+2 )")
      CompareDecimals(outputBlock, Left, 123.4561D, "123.4561D")
      CompareDecimals(outputBlock, Left, 123.4559D, "123.4559D")
      CompareDecimals(outputBlock, Left, 123.456D, "123.456000D")
      CompareDecimals(outputBlock, Left, _
          New Decimal(123456000, 0, 0, False, 6), _
          "Decimal( 123456000, 0, 0, false, 6 )")
   End Sub
End Module

' This example of the Decimal.Equals( Decimal, Decimal ) and
' Decimal.Compare( Decimal, Decimal ) methods generates the
' following output. It creates several different Decimal
' values and compares them with the following reference value.
' 
' Left: Decimal( 123.456 )                     123.456
' 
' Right: Decimal( 1.2345600E+2 )               123.456
' Decimal.Equals( Left, Right )                True
' Decimal.Compare( Left, Right )               0
' 
' Right: 123.4561D                             123.4561
' Decimal.Equals( Left, Right )                False
' Decimal.Compare( Left, Right )               -1
' 
' Right: 123.4559D                             123.4559
' Decimal.Equals( Left, Right )                False
' Decimal.Compare( Left, Right )               1
' 
' Right: 123.456000D                           123.456
' Decimal.Equals( Left, Right )                True
' Decimal.Compare( Left, Right )               0
' 
' Right: Decimal( 123456000, 0, 0, false, 6 )  123.456000
' Decimal.Equals( Left, Right )                True
' Decimal.Compare( Left, Right )               0
// Example of the decimal.Compare and static decimal.Equals methods.
using System;

class Example
{
   const string dataFmt = "{0,-45}{1}";

   // Compare decimal parameters, and display them with the results.
   public static void CompareDecimals(System.Windows.Controls.TextBlock outputBlock, decimal Left, decimal Right,
       string RightText)
   {
      outputBlock.Text += "\n";
      outputBlock.Text += String.Format(dataFmt, "Right: " + RightText, Right) + "\n";
      outputBlock.Text += String.Format(dataFmt, "decimal.Equals( Left, Right )",
          Decimal.Equals(Left, Right)) + "\n";
      outputBlock.Text += String.Format(dataFmt, "decimal.Compare( Left, Right )",
          Decimal.Compare(Left, Right)) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "This example of the " +
          "decimal.Equals( decimal, decimal ) and \n" +
          "decimal.Compare( decimal, decimal ) methods " +
          "generates the \nfollowing output. It creates several " +
          "different decimal \nvalues and compares them with " +
          "the following reference value.\n" + "\n";

      // Create a reference decimal value.
      decimal Left = new decimal(123.456);

      outputBlock.Text += String.Format(dataFmt, "Left: decimal( 123.456 )",
          Left) + "\n";

      // Create decimal values to compare with the reference.
      CompareDecimals(outputBlock, Left, new decimal(1.2345600E+2),
          "decimal( 1.2345600E+2 )");
      CompareDecimals(outputBlock, Left, 123.4561M, "123.4561M");
      CompareDecimals(outputBlock, Left, 123.4559M, "123.4559M");
      CompareDecimals(outputBlock, Left, 123.456000M, "123.456000M");
      CompareDecimals(outputBlock, Left,
          new decimal(123456000, 0, 0, false, 6),
          "decimal( 123456000, 0, 0, false, 6 )");
   }
}

/*
This example of the decimal.Equals( decimal, decimal ) and
decimal.Compare( decimal, decimal ) methods generates the
following output. It creates several different decimal
values and compares them with the following reference value.

Left: decimal( 123.456 )                     123.456

Right: decimal( 1.2345600E+2 )               123.456
decimal.Equals( Left, Right )                True
decimal.Compare( Left, Right )               0

Right: 123.4561M                             123.4561
decimal.Equals( Left, Right )                False
decimal.Compare( Left, Right )               -1

Right: 123.4559M                             123.4559
decimal.Equals( Left, Right )                False
decimal.Compare( Left, Right )               1

Right: 123.456000M                           123.456000
decimal.Equals( Left, Right )                True
decimal.Compare( Left, Right )               0

Right: decimal( 123456000, 0, 0, false, 6 )  123.456000
decimal.Equals( Left, Right )                True
decimal.Compare( Left, Right )               0
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.