Compare Method

# Decimal.Compare Method

.NET Framework 1.1

Compares two specified Decimal values.

```[Visual Basic]
Public Shared Function Compare( _
ByVal d1 As Decimal, _
ByVal d2 As Decimal _
) As Integer
[C#]
public static int Compare(
decimal d1,
decimal d2
);
[C++]
public: static int Compare(
Decimal d1,
Decimal d2
);
[JScript]
public static function Compare(
d1 : Decimal,
d2 : Decimal
) : int;```

d1
A Decimal.
d2
A Decimal.

#### Return Value

A signed number indicating the relative values of 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.

#### Example

[Visual Basic, C#, C++] The following code example compares several Decimal values to a reference Decimal value using the Compare method.

```[Visual Basic]
' Example of the Decimal.Compare and static Decimal.Equals methods.
Imports System
Imports Microsoft.VisualBasic

Module DecCompareEqualsDemo

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

' Compare Decimal parameters, and display them with the results.
Sub CompareDecimals( Left as Decimal, Right as Decimal, _
RightText as String )

Console.WriteLine( )
Console.WriteLine( dataFmt, "Right: " & RightText, Right )
Console.WriteLine( dataFmt, "Decimal.Equals( Left, Right )", _
Decimal.Equals( Left, Right ) )
Console.WriteLine( dataFmt, _
"Decimal.Compare( Left, Right )", _
Decimal.Compare( Left, Right ) )
End Sub

Sub Main( )
Console.WriteLine( _
"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 )

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

Console.WriteLine( dataFmt, "Left: Decimal( 123.456 )", Left )

' Create Decimal values to compare with the reference.
CompareDecimals( Left, New Decimal( 1.2345600E+2 ), _
"Decimal( 1.2345600E+2 )" )
CompareDecimals( Left, 123.4561D, "123.4561D" )
CompareDecimals( Left, 123.4559D, "123.4559D" )
CompareDecimals( Left, 123.456000D, "123.456000D" )
CompareDecimals( 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

[C#]
// Example of the decimal.Compare and static decimal.Equals methods.
using System;

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

// Compare decimal parameters, and display them with the results.
public static void CompareDecimals( decimal Left, decimal Right,
string RightText )
{
Console.WriteLine( );
Console.WriteLine( dataFmt, "Right: "+RightText, Right );
Console.WriteLine( dataFmt, "decimal.Equals( Left, Right )",
Decimal.Equals( Left, Right ) );
Console.WriteLine( dataFmt, "decimal.Compare( Left, Right )",
Decimal.Compare( Left, Right ) );
}

public static void Main( )
{
Console.WriteLine( "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" );

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

Console.WriteLine( dataFmt, "Left: decimal( 123.456 )",
Left );

// Create decimal values to compare with the reference.
CompareDecimals( Left, new decimal( 1.2345600E+2 ),
"decimal( 1.2345600E+2 )" );
CompareDecimals( Left, 123.4561M, "123.4561M" );
CompareDecimals( Left, 123.4559M, "123.4559M" );
CompareDecimals( Left, 123.456000M, "123.456000M" );
CompareDecimals( 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
*/

[C++]
// Example of the Decimal::Compare and static Decimal::Equals methods.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* protoFmt = L"{0,-45}{1}" ;

// Compare Decimal parameters, and display them with the results.
void CompareDecimals( Decimal Left, Decimal Right,
String* RightText )
{
String* dataFmt = new String( protoFmt );

Console::WriteLine( );
Console::WriteLine( dataFmt,
String::Concat( S"Right: ", RightText ), __box( Right ) );
Console::WriteLine( dataFmt, S"Decimal::Equals( Left, Right )",
__box( Decimal::Equals( Left, Right ) ) );
Console::WriteLine( dataFmt, S"Decimal::Compare( Left, Right )",
__box( Decimal::Compare( Left, Right ) ) );
}

void main( )
{
Console::WriteLine(
S"This example of the Decimal::Equals( Decimal, Decimal "
S") and \nDecimal::Compare( Decimal, Decimal ) "
S"methods generates the \nfollowing output. It creates "
S"several different Decimal \nvalues and compares them "
S"with the following reference value.\n" );

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

Console::WriteLine( new String( protoFmt ),
S"Left: Decimal( 123.456 )", __box( Left ) );

// Create Decimal values to compare with the reference.
CompareDecimals( Left, Decimal( 1.2345600E+2 ),
S"Decimal( 1.2345600E+2 )" );
CompareDecimals( Left, Decimal::Parse( S"123.4561" ),
S"Decimal::Parse( \"123.4561\" )" );
CompareDecimals( Left, Decimal::Parse( S"123.4559" ),
S"Decimal::Parse( \"123.4559\" )");
CompareDecimals( Left, Decimal::Parse( S"123.456000" ),
S"Decimal::Parse( \"123.456000\" )" );
CompareDecimals( Left, Decimal( 123456000, 0, 0, false, 6 ),
S"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: Decimal::Parse( "123.4561" )          123.4561
Decimal::Equals( Left, Right )               False
Decimal::Compare( Left, Right )              -1

Right: Decimal::Parse( "123.4559" )          123.4559
Decimal::Equals( Left, Right )               False
Decimal::Compare( Left, Right )              1

Right: Decimal::Parse( "123.456000" )        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
*/
```

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.

#### Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard