Export (0) Print
Expand All

Decimal Multiplication Operator

Multiplies two specified Decimal values.

[Visual Basic]
returnValue = Decimal.op_Multiply(d1, d2)
[C#]
public static decimal operator *(
 decimal d1,
 decimal d2
);
[C++]
public: static Decimal op_Multiply(
 Decimal d1,
 Decimal d2
);
[JScript]
returnValue = d1 * d2;

[Visual Basic] In Visual Basic, you can use the operators defined by a type, but you cannot define your own. You can use the Multiply method instead of the Decimal multiplication operator.

[JScript] In JScript, you can use the operators defined by a type, but you cannot define your own.

Arguments [Visual Basic, JScript]

d1
A Decimal.
d2
A Decimal.

Parameters [C#, C++]

d1
A Decimal.
d2
A Decimal.

Return Value

The Decimal result of multiplying d1 by d2.

Exceptions

Exception Type Condition
OverflowException The return value is less than MinValue or greater than MaxValue.

Example

[Visual Basic, C#, C++] The following code example creates several pairs of Decimal values and calculates their products with the Multiplication operator.

[Visual Basic] 
' Example of the Decimal multiplication, division, and modulus 
' operators.
Imports System
Imports Microsoft.VisualBasic

Module DecimalMulDivRemOpsDemo
    
    Const dataFmt As String = "{0,-38}{1,31}"

    ' Display Decimal parameters and their product, quotient, and 
    ' remainder.
    Sub ShowDecimalProQuoRem( Left as Decimal, Right as Decimal )

        Console.WriteLine( )
        Console.WriteLine( dataFmt, "Decimal Left", Left )
        Console.WriteLine( dataFmt, "Decimal Right", Right )

        ' The op_Multiply, op_Division, and op_Modulus operators must 
        ' be explicitly coded in Visual Basic. If binary *, /, or 
        ' Mod are used, other methods are called.
        Console.WriteLine( dataFmt, _
            "Decimal.op_Multiply( Left, Right )", _
            Decimal.op_Multiply( Left, Right ) )
        Console.WriteLine( dataFmt, _
            "Decimal.op_Division( Left, Right )", _
            Decimal.op_Division( Left, Right ) )
        Console.WriteLine( dataFmt, _
            "Decimal.op_Modulus( Left, Right )", _
            Decimal.op_Modulus( Left, Right ) )
    End Sub

    Sub Main( )
        Console.WriteLine( _
            "This example of the Decimal multiplication, " & _
            "division, and modulus " & vbCrLf & "operators " & _
            "generates the following output. It displays the " & _
            "product, " & vbCrLf & "quotient, and remainder of " & _
            "several pairs of Decimal objects." )

        ' Create pairs of Decimal objects.
        ShowDecimalProQuoRem( 1000D, 7D ) 
        ShowDecimalProQuoRem( -1000D, 7D ) 
        ShowDecimalProQuoRem( _
            new Decimal( 1230000000, 0, 0, False, 7 ), _
            0.0012300D )
        ShowDecimalProQuoRem( 12345678900000000D, _
            0.0000000012345678D )
        ShowDecimalProQuoRem( 123456789.0123456789D, _
            123456789.1123456789D )
    End Sub
End Module 

' This example of the Decimal multiplication, division, and modulus
' operators generates the following output. It displays the product,
' quotient, and remainder of several pairs of Decimal objects.
' 
' Decimal Left                                                     1000
' Decimal Right                                                       7
' Decimal.op_Multiply( Left, Right )                               7000
' Decimal.op_Division( Left, Right )     142.85714285714285714285714286
' Decimal.op_Modulus( Left, Right )                                   6
' 
' Decimal Left                                                    -1000
' Decimal Right                                                       7
' Decimal.op_Multiply( Left, Right )                              -7000
' Decimal.op_Division( Left, Right )    -142.85714285714285714285714286
' Decimal.op_Modulus( Left, Right )                                  -6
' 
' Decimal Left                                              123.0000000
' Decimal Right                                                 0.00123
' Decimal.op_Multiply( Left, Right )                     0.151290000000
' Decimal.op_Division( Left, Right )                          100000.00
' Decimal.op_Modulus( Left, Right )                                   0
' 
' Decimal Left                                        12345678900000000
' Decimal Right                                      0.0000000012345678
' Decimal.op_Multiply( Left, Right )          15241577.6390794200000000
' Decimal.op_Division( Left, Right )     10000000729000059778004901.796
' Decimal.op_Modulus( Left, Right )                      0.000000000983
' 
' Decimal Left                                     123456789.0123456789
' Decimal Right                                    123456789.1123456789
' Decimal.op_Multiply( Left, Right )     15241578765584515.651425087878
' Decimal.op_Division( Left, Right )     0.9999999991899999933660999449
' Decimal.op_Modulus( Left, Right )                123456789.0123456789

[C#] 
// Example of the decimal multiplication, division, and modulus 
// operators.
using System;

class DecimalMulDivRemOpsDemo
{
    const string dataFmt = "   {0,-18}{1,31}";

    // Display decimal parameters and their product, quotient, and 
    // remainder.
    public static void ShowDecimalProQuoRem( decimal Left, decimal Right )
    {
        Console.WriteLine( );
        Console.WriteLine( dataFmt, "decimal Left", Left );
        Console.WriteLine( dataFmt, "decimal Right", Right );
        Console.WriteLine( dataFmt, "Left * Right", Left * Right );
        Console.WriteLine( dataFmt, "Left / Right", Left / Right );
        Console.WriteLine( dataFmt, "Left % Right", Left % Right );
    }

    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the decimal multiplication, division, " +
            "and modulus \noperators generates the following " +
            "output. It displays the product, \nquotient, and " +
            "remainder of several pairs of decimal objects." );

        // Create pairs of decimal objects.
        ShowDecimalProQuoRem( 1000M, 7M );
        ShowDecimalProQuoRem( -1000M, 7M );
        ShowDecimalProQuoRem( 
            new decimal( 1230000000, 0, 0, false, 7 ), 0.0012300M );
        ShowDecimalProQuoRem( 12345678900000000M, 
            0.0000000012345678M );
        ShowDecimalProQuoRem( 123456789.0123456789M, 
            123456789.1123456789M );
    }
}

/*
This example of the decimal multiplication, division, and modulus
operators generates the following output. It displays the product,
quotient, and remainder of several pairs of decimal objects.

   decimal Left                                 1000
   decimal Right                                   7
   Left * Right                                 7000
   Left / Right       142.85714285714285714285714286
   Left % Right                                    6

   decimal Left                                -1000
   decimal Right                                   7
   Left * Right                                -7000
   Left / Right      -142.85714285714285714285714286
   Left % Right                                   -6

   decimal Left                          123.0000000
   decimal Right                           0.0012300
   Left * Right                     0.15129000000000
   Left / Right                               100000
   Left % Right                                    0

   decimal Left                    12345678900000000
   decimal Right                  0.0000000012345678
   Left * Right            15241577.6390794200000000
   Left / Right       10000000729000059778004901.796
   Left % Right                       0.000000000983

   decimal Left                 123456789.0123456789
   decimal Right                123456789.1123456789
   Left * Right       15241578765584515.651425087878
   Left / Right       0.9999999991899999933660999449
   Left % Right                 123456789.0123456789
*/

[C++] 
// Example of the Decimal multiplication, division, and modulus 
// operators.
#using <mscorlib.dll>
using namespace System;

// Display Decimal parameters and their product, quotient, and 
// remainder.
void ShowDecimalProQuoRem( Decimal Left, Decimal Right )
{
    String* dataFmt = S"   {0,-18}{1,31}";

    Console::WriteLine( );
    Console::WriteLine( dataFmt, S"Decimal Left", __box( Left ) );
    Console::WriteLine( dataFmt, S"Decimal Right", __box( Right ) );
    Console::WriteLine( dataFmt, S"Left * Right", 
        __box( Left * Right ) );
    Console::WriteLine( dataFmt, S"Left / Right", 
        __box( Left / Right ) );
    Console::WriteLine( dataFmt, S"Left % Right", 
        __box( Left % Right ) );
}

void main( )
{
    Console::WriteLine( 
        S"This example of the Decimal multiplication, division, " 
        S"and modulus \noperators generates the following " 
        S"output. It displays the product, \nquotient, and " 
        S"remainder of several pairs of Decimal objects." );

    // Create pairs of Decimal objects.
    ShowDecimalProQuoRem( Decimal::Parse( S"1000" ), 
        Decimal::Parse( S"7" ) );
    ShowDecimalProQuoRem( Decimal::Parse( S"-1000" ), 
        Decimal::Parse( S"7" ) );
    ShowDecimalProQuoRem( 
        Decimal( 1230000000, 0, 0, false, 7 ), 
        Decimal::Parse( S"0.0012300" ) );
    ShowDecimalProQuoRem( Decimal::Parse( S"12345678900000000" ), 
        Decimal::Parse( S"0.0000000012345678" ) );
    ShowDecimalProQuoRem( Decimal::Parse( S"123456789.0123456789" ), 
        Decimal::Parse( S"123456789.1123456789" ) );
}

/*
This example of the Decimal multiplication, division, and modulus
operators generates the following output. It displays the product,
quotient, and remainder of several pairs of Decimal objects.

   Decimal Left                                 1000
   Decimal Right                                   7
   Left * Right                                 7000
   Left / Right       142.85714285714285714285714286
   Left % Right                                    6

   Decimal Left                                -1000
   Decimal Right                                   7
   Left * Right                                -7000
   Left / Right      -142.85714285714285714285714286
   Left % Right                                   -6

   Decimal Left                          123.0000000
   Decimal Right                           0.0012300
   Left * Right                     0.15129000000000
   Left / Right                               100000
   Left % Right                                    0

   Decimal Left                    12345678900000000
   Decimal Right                  0.0000000012345678
   Left * Right            15241577.6390794200000000
   Left / Right       10000000729000059778004901.796
   Left % Right                       0.000000000983

   Decimal Left                 123456789.0123456789
   Decimal Right                123456789.1123456789
   Left * Right       15241578765584515.651425087878
   Left / Right       0.9999999991899999933660999449
   Left % Right                 123456789.0123456789
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter 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

See Also

Decimal Structure | Decimal Members | System Namespace | Multiply

Show:
© 2014 Microsoft