The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
Decimal::CompareTo Method (Object^)
.NET Framework (current version)
Compares this instance to a specified object and returns a comparison of their relative values.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
-
Type:
System::Object^
The object to compare with this instance, or null.
Return Value
Type: System::Int32A signed number indicating the relative values of this instance and value.
Return value | Meaning |
|---|---|
Less than zero | This instance is less than value. |
Zero | This instance is equal to value. |
Greater than zero | This instance is greater than value. -or- value is null. |
Implements
IComparable::CompareTo(Object^)| Exception | Condition |
|---|---|
| ArgumentException | value is not a Decimal. |
The following code example compares several Decimal and other objects to a reference Decimal value using the CompareTo method.
// Example of the Decimal::CompareTo and Decimal::Equals instance // methods. using namespace System; // Get the exception type name; remove the namespace prefix. String^ GetExceptionType( Exception^ ex ) { String^ exceptionType = ex->GetType()->ToString(); return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 ); } // Compare the Decimal to the Object parameters, // and display the Object parameters with the results. void CompDecimalToObject( Decimal Left, Object^ Right, String^ RightText ) { Console::WriteLine( "{0,-46}{1}", String::Concat( "Object: ", RightText ), Right ); Console::WriteLine( "{0,-46}{1}", "Left.Equals( Object )", Left.Equals( Right ) ); Console::Write( "{0,-46}", "Left.CompareTo( Object )" ); try { // Catch the exception if CompareTo( ) throws one. Console::WriteLine( "{0}\n", Left.CompareTo( Right ) ); } catch ( Exception^ ex ) { Console::WriteLine( "{0}\n", GetExceptionType( ex ) ); } } int main() { Console::WriteLine( "This example of the Decimal::Equals( Object* ) and \n" "Decimal::CompareTo( Object* ) methods generates the \n" "following output. It creates several different " "Decimal \nvalues and compares them with the following " "reference value.\n" ); // Create a reference Decimal value. Decimal Left = Decimal(987.654); Console::WriteLine( "{0,-46}{1}\n", "Left: Decimal( 987.654 )", Left ); // Create objects to compare with the reference. CompDecimalToObject( Left, Decimal(9.8765400E+2), "Decimal( 9.8765400E+2 )" ); CompDecimalToObject( Left, Decimal::Parse( "987.6541" ), "Decimal::Parse( \"987.6541\" )" ); CompDecimalToObject( Left, Decimal::Parse( "987.6539" ), "Decimal::Parse( \"987.6539\" )" ); CompDecimalToObject( Left, Decimal(987654000,0,0,false,6), "Decimal( 987654000, 0, 0, false, 6 )" ); CompDecimalToObject( Left, 9.8765400E+2, "Double 9.8765400E+2" ); CompDecimalToObject( Left, "987.654", "String \"987.654\"" ); } /* This example of the Decimal::Equals( Object* ) and Decimal::CompareTo( Object* ) methods generates the following output. It creates several different Decimal values and compares them with the following reference value. Left: Decimal( 987.654 ) 987.654 Object: Decimal( 9.8765400E+2 ) 987.654 Left.Equals( Object ) True Left.CompareTo( Object ) 0 Object: Decimal::Parse( "987.6541" ) 987.6541 Left.Equals( Object ) False Left.CompareTo( Object ) -1 Object: Decimal::Parse( "987.6539" ) 987.6539 Left.Equals( Object ) False Left.CompareTo( Object ) 1 Object: Decimal( 987654000, 0, 0, false, 6 ) 987.654000 Left.Equals( Object ) True Left.CompareTo( Object ) 0 Object: Double 9.8765400E+2 987.654 Left.Equals( Object ) False Left.CompareTo( Object ) ArgumentException Object: String "987.654" 987.654 Left.Equals( Object ) False Left.CompareTo( Object ) ArgumentException */
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Show: