Export (0) Print
Expand All

Decimal.op_Decrement Method

Decrements the Decimal operand by one.

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

public static decimal operator -- (
	decimal d
)
J# does not support overloaded operators.
JScript supports the use of overloaded operators, but not the declaration of new ones.

Parameters

d

The Decimal operand.

Return Value

The value of d decremented by 1.

Exception typeCondition

OverflowException

The return value is less than MinValue or greater than MaxValue.

The following code example applies the Decrement operator to several Decimal values.

// Example of the decimal increment, decrement, unary negation, and 
// unary plus operators.
using System;

class DecIncrDecrUnaryDemo
{
    // Get the exception type name; remove the namespace prefix.
    public static string GetExceptionType( Exception ex ) 
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    // Display the argument and the incremented and decremented values.
    public static void DecIncrDecrUnary( decimal argument )
    {
        decimal toBeIncr = argument;
        decimal toBeDecr = argument;

        Console.WriteLine( "{0,-26}{1}", "decimal argument: ", 
            argument );

        // Catch the exception if the increment operator throws one.
        Console.Write( "{0,-26}", "argument ++" );
        try
        {
            toBeIncr ++;
            Console.WriteLine( "{0}", toBeIncr );
        }
        catch( Exception ex )
        {
            Console.WriteLine( "{0}", GetExceptionType( ex ) );
        }

        // Catch the exception if the decrement operator throws one.
        Console.Write( "{0,-26}", "argument --" );
        try
        {
            toBeDecr --;
            Console.WriteLine( "{0}", toBeDecr );
        }
        catch( Exception ex )
        {
            Console.WriteLine( "{0}", GetExceptionType( ex ) );
        }
            
        Console.WriteLine( );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the decimal increment, " +
            "decrement, unary negation, \nand unary plus operators " +
            "generates the following output. It \ndisplays the " +
            "results of the operators on several decimal values.\n" );

        // Create objects to compare with the reference.
        DecIncrDecrUnary( 0.000000123M );
        DecIncrDecrUnary( new decimal( 123000000, 0, 0, false, 9 ) );
        DecIncrDecrUnary( - new decimal( 123000000, 0, 0, false, 9 ) );
        DecIncrDecrUnary( + decimal.MaxValue );
        DecIncrDecrUnary( - decimal.MaxValue );
        DecIncrDecrUnary( + 7.5000000000000000000000000001M );
    }
}

/*
This example of the decimal increment, decrement, unary negation,
and unary plus operators generates the following output. It
displays the results of the operators on several decimal values.

decimal argument:         0.000000123
argument ++               1.000000123
argument --               -0.999999877

decimal argument:         0.123000000
argument ++               1.123000000
argument --               -0.877000000

decimal argument:         -0.123000000
argument ++               0.877000000
argument --               -1.123000000

decimal argument:         79228162514264337593543950335
argument ++               OverflowException
argument --               79228162514264337593543950334

decimal argument:         -79228162514264337593543950335
argument ++               -79228162514264337593543950334
argument --               OverflowException

decimal argument:         7.5000000000000000000000000001
argument ++               8.500000000000000000000000000
argument --               6.5000000000000000000000000001
*/

// Example of the decimal increment, decrement, unary negation, and 
// unary plus operators.
import System.*;

class DecIncrDecrUnaryDemo
{
    // Get the exception type name; remove the namespace prefix.
    public static String GetExceptionType(Exception ex)
    {
        String exceptionType = ex.GetType().ToString();
        return exceptionType.Substring((exceptionType.LastIndexOf('.') + 1));
    } //GetExceptionType

    // Display the argument and the incremented and decremented values.
    public static void DecIncrDecrUnary(System.Decimal argument)
    {
        System.Decimal toBeIncr = argument;
        System.Decimal toBeDecr = argument;
        
        Console.WriteLine("{0,-26}{1}", "decimal argument: ", argument);
        
        // Catch the exception if the increment operator throws one.
        Console.Write("{0,-26}", "argument ++");
        try {
            toBeIncr =System.Decimal.Add(toBeIncr,System.Convert.ToDecimal(1));
            Console.WriteLine("{0}", toBeIncr);
        }
        catch(System.Exception  ex){        
            Console.WriteLine("{0}",(ex.GetType()));
        }
        
        // Catch the exception if the decrement operator throws one.
        Console.Write("{0,-26}", "argument --");
        try {
            toBeDecr= System.Decimal.Subtract(toBeIncr,
                System.Convert.ToDecimal(1));
            Console.WriteLine("{0}", toBeDecr);
        }
        catch(System.Exception  ex){        
            Console.WriteLine("{0}", (ex.GetType()));
        }        
        Console.WriteLine();
    } //DecIncrDecrUnary

    public static void main(String[] args)
    {

        Console.WriteLine(("This example of the decimal increment, " 
            + "decrement, unary negation, \nand unary plus operators " 
            + "generates the following output. It \ndisplays the " 
            + "results of the operators on several decimal values.\n"));
        
        // Create objects to compare with the reference.
        DecIncrDecrUnary(System.Convert.ToDecimal(0.000000123));
        DecIncrDecrUnary(new System.Decimal(123000000, 0, 0, false,
            System.Convert.ToByte(9)));
        DecIncrDecrUnary(System.Decimal.Negate(new System.Decimal(123000000,
            0, 0, false,System.Convert.ToByte(9))));
        DecIncrDecrUnary(System.Decimal.MaxValue);
        DecIncrDecrUnary(System.Decimal.Negate(System.Decimal.MaxValue));
        DecIncrDecrUnary(System.Convert.ToDecimal
            (7.5000000000000000000000000001));
    } //main
} //DecIncrDecrUnaryDemo

/*
decimal argument:         0.000000123
argument ++               1.000000123
argument --               0.000000123

decimal argument:         0.123000000
argument ++               1.123000000
argument --               0.123000000

decimal argument:         -0.123000000
argument ++               0.877000000
argument --               -0.123000000

decimal argument:         79228162514264337593543950335
argument ++               System.OverflowException
argument --               79228162514264337593543950334

decimal argument:         -79228162514264337593543950335
argument ++               -79228162514264337593543950334
argument --               -79228162514264337593543950335

decimal argument:         7.5
argument ++               8.5
argument --               7.5
*/

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft