Export (0) Print
Expand All

Decimal.Equals Method (Decimal, Decimal)

Returns a value indicating whether two specified instances of Decimal represent the same value.

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

public static bool Equals (
	decimal d1,
	decimal d2
)
public static boolean Equals (
	Decimal d1, 
	Decimal d2
)
public static function Equals (
	d1 : decimal, 
	d2 : decimal
) : boolean
Not applicable.

Parameters

d1

A Decimal.

d2

A Decimal.

Return Value

true if d1 and d2 are equal; otherwise, false.

The following code example compares several Decimal values to a reference Decimal value using the static Equals 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
*/ 

// Example of the decimal.Compare and static decimal.Equals methods.
import System.* ;

class DecCompareEqualsDemo
{
    private static String dataFmt = "{0,-45}{1}";   
   
    // Compare decimal parameters, and display them with the results.
    public static void CompareDecimals(System.Decimal left, 
        System.Decimal right, String rightText)
    {
        Console.WriteLine();
        Console.WriteLine(dataFmt, "Right: " + rightText,right);
        Console.WriteLine(dataFmt, "decimal.Equals( Left, Right )",
            System.Convert.ToString(Decimal.Equals(left,right)));    
        Console.WriteLine(dataFmt, "decimal.Compare( Left, Right )",
            System.Convert.ToString(Decimal.Compare(left,right)));
    } //CompareDecimals   
   
    public static void main(String[] args)
    {
        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.
        System.Decimal left =  new System.Decimal(123.456);
      
        Console.WriteLine(dataFmt, "Left: decimal( 123.456 )",left);
      
        // Create decimal values to compare with the reference.
        CompareDecimals(left, 
            new System.Decimal(123.456), "decimal( 1.2345600E+2 )");
        CompareDecimals(left, System.Convert.ToDecimal(123.4561), "123.4561");
        CompareDecimals(left, System.Convert.ToDecimal(123.4559), "123.4559");
        CompareDecimals(left,System.Convert.ToDecimal(123.456000),"123.456000");
        CompareDecimals(left, new System.Decimal(123456000, 0, 0, false,
            System.Convert.ToByte(6)), "decimal( 123456000, 0, 0, false, 6 )");
    } //main
} //DecCompareEqualsDemo

/*
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.4561                              123.4561
decimal.Equals( Left, Right )                False
decimal.Compare( Left, Right )               -1

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

Right: 123.456000                            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
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft