Export (0) Print
Expand All

Decimal.Compare Method

Compares two specified Decimal values.

Namespace:  System
Assemblies:   mscorlib (in mscorlib.dll)
  System.Runtime (in System.Runtime.dll)

public static int Compare(
	decimal d1,
	decimal d2
)

Parameters

d1
Type: System.Decimal

The first value to compare.

d2
Type: System.Decimal

The second value to compare.

Return Value

Type: System.Int32
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.

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

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft