Share via


Decimal Constructor (Int32, Int32, Int32, Boolean, Byte)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of Decimal from parameters specifying the instance's constituent parts.

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

Syntax

'Declaration
Public Sub New ( _
    lo As Integer, _
    mid As Integer, _
    hi As Integer, _
    isNegative As Boolean, _
    scale As Byte _
)
public Decimal(
    int lo,
    int mid,
    int hi,
    bool isNegative,
    byte scale
)

Parameters

  • lo
    Type: System.Int32
    The low 32 bits of a 96-bit integer.
  • mid
    Type: System.Int32
    The middle 32 bits of a 96-bit integer.
  • hi
    Type: System.Int32
    The high 32 bits of a 96-bit integer.
  • isNegative
    Type: System.Boolean
    The sign of the number; 1 is negative, 0 is positive.
  • scale
    Type: System.Byte
    A power of 10 ranging from 0 to 28.

Exceptions

Exception Condition
ArgumentOutOfRangeException

scale is greater than 28.

Remarks

The binary representation of a Decimal number consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the integer number and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10 raised to an exponent ranging from 0 to 28.

Examples

The following code example creates several Decimal numbers using the constructor overload that initializes a Decimal structure with three Int32 value words, a Boolean sign, and a Byte scale factor.

' Example of the Decimal( Integer, Integer, Integer, Boolean, Byte ) 
' constructor.

Module Example

   ' Get the exception type name; remove the namespace prefix.
   Function GetExceptionType(ByVal ex As Exception) As String

      Dim exceptionType As String = ex.GetType().ToString()
      Return exceptionType.Substring( _
          exceptionType.LastIndexOf("."c) + 1)
   End Function

   ' Create a Decimal object and display its value.
   Sub CreateDecimal(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal low As Integer, ByVal mid As Integer, _
       ByVal high As Integer, ByVal isNeg As Boolean, ByVal scale As Byte)

      ' Format the constructor for display.
      Dim ctor As String = String.Format( _
          "Decimal( {0}, {1}, {2}, {3}, {4} )", _
          low, mid, high, isNeg, scale)
      Dim valOrExc As String

      ' Construct the Decimal value.
      Try
         Dim decimalNum As New Decimal( _
             low, mid, high, isNeg, scale)

         ' Format and save the Decimal value.
         valOrExc = decimalNum.ToString()

         ' Save the exception type if an exception was thrown.
      Catch ex As Exception
         valOrExc = GetExceptionType(ex)
      End Try

      ' Display the constructor and Decimal value or exception.
      Dim ctorLen = 76 - valOrExc.Length
      If ctorLen > ctor.Length Then

         ' Display the data on one line if it will fit.
         outputBlock.Text &= String.Format("{0}{1}", ctor.PadRight(ctorLen) & vbCrLf, _
             valOrExc)

         ' Otherwise, display the data on two lines.
      Else
         outputBlock.Text &= String.Format("{0}", ctor) & vbCrLf
         outputBlock.Text &= String.Format("{0,76}", valOrExc) & vbCrLf
      End If
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= _
          "This example of the Decimal( Integer, Integer, " & _
          "Integer, Boolean, Byte ) " & vbCrLf & "constructor " & _
          "generates the following output." & vbCrLf & vbCrLf
      outputBlock.Text &= "{0,-38}{1,38}", "Constructor", _
          "Value or Exception" & vbCrLf
      outputBlock.Text &= "{0,-38}{1,38}", "-----------", _
          "------------------" & vbCrLf

      ' Construct Decimal objects from the component fields.
      CreateDecimal(outputBlock, 0, 0, 0, False, 0)
      CreateDecimal(outputBlock, 0, 0, 0, False, 27)
      CreateDecimal(outputBlock, 0, 0, 0, True, 0)
      CreateDecimal(outputBlock, 1000000000, 0, 0, False, 0)
      CreateDecimal(outputBlock, 0, 1000000000, 0, False, 0)
      CreateDecimal(outputBlock, 0, 0, 1000000000, False, 0)
      CreateDecimal(outputBlock, 1000000000, 1000000000, 1000000000, False, 0)
      CreateDecimal(outputBlock, -1, -1, -1, False, 0)
      CreateDecimal(outputBlock, -1, -1, -1, True, 0)
      CreateDecimal(outputBlock, -1, -1, -1, False, 15)
      CreateDecimal(outputBlock, -1, -1, -1, False, 28)
      CreateDecimal(outputBlock, -1, -1, -1, False, 29)
      CreateDecimal(outputBlock, Integer.MaxValue, 0, 0, False, 18)
      CreateDecimal(outputBlock, Integer.MaxValue, 0, 0, False, 28)
      CreateDecimal(outputBlock, Integer.MaxValue, 0, 0, True, 28)
   End Sub
End Module

' This example of the Decimal( Integer, Integer, Integer, Boolean, Byte )
' constructor generates the following output.
' 
' Constructor                                               Value or Exception
' -----------                                               ------------------
' Decimal( 0, 0, 0, False, 0 )                                               0
' Decimal( 0, 0, 0, False, 27 )                                              0
' Decimal( 0, 0, 0, True, 0 )                                                0
' Decimal( 1000000000, 0, 0, False, 0 )                             1000000000
' Decimal( 0, 1000000000, 0, False, 0 )                    4294967296000000000
' Decimal( 0, 0, 1000000000, False, 0 )          18446744073709551616000000000
' Decimal( 1000000000, 1000000000, 1000000000, False, 0 )
'                                                18446744078004518913000000000
' Decimal( -1, -1, -1, False, 0 )                79228162514264337593543950335
' Decimal( -1, -1, -1, True, 0 )                -79228162514264337593543950335
' Decimal( -1, -1, -1, False, 15 )              79228162514264.337593543950335
' Decimal( -1, -1, -1, False, 28 )              7.9228162514264337593543950335
' Decimal( -1, -1, -1, False, 29 )                 ArgumentOutOfRangeException
' Decimal( 2147483647, 0, 0, False, 18 )                  0.000000002147483647
' Decimal( 2147483647, 0, 0, False, 28 )        0.0000000000000000002147483647
' Decimal( 2147483647, 0, 0, True, 28 )        -0.0000000000000000002147483647
// Example of the decimal( int, int, int, bool, byte ) constructor.
using System;

class Example
{
   // 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);
   }

   // Create a decimal object and display its value.
   public static void CreateDecimal(System.Windows.Controls.TextBlock outputBlock, int low, int mid, int high,
       bool isNeg, byte scale)
   {
      // Format the constructor for display.
      string ctor = String.Format(
          "decimal( {0}, {1}, {2}, {3}, {4} )",
          low, mid, high, isNeg, scale);
      string valOrExc;

      try
      {
         // Construct the decimal value.
         decimal decimalNum = new decimal(
             low, mid, high, isNeg, scale);

         // Format and save the decimal value.
         valOrExc = decimalNum.ToString();
      }
      catch (Exception ex)
      {
         // Save the exception type if an exception was thrown.
         valOrExc = GetExceptionType(ex);
      }

      // Display the constructor and decimal value or exception.
      int ctorLen = 76 - valOrExc.Length;

      // Display the data on one line if it will fit.
      if (ctorLen > ctor.Length)
         outputBlock.Text += String.Format("{0}{1}", ctor.PadRight(ctorLen),
             valOrExc) + "\n";

      // Otherwise, display the data on two lines.
      else
      {
         outputBlock.Text += String.Format("{0}", ctor) + "\n";
         outputBlock.Text += String.Format("{0,76}", valOrExc) + "\n";
      }
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      outputBlock.Text += String.Format("This example of the decimal( int, int, " +
          "int, bool, byte ) \nconstructor " +
          "generates the following output.\n") + "\n";
      outputBlock.Text += String.Format("{0,-38}{1,38}", "Constructor",
          "Value or Exception") + "\n";
      outputBlock.Text += String.Format("{0,-38}{1,38}", "-----------",
          "------------------") + "\n";

      // Construct decimal objects from the component fields.
      CreateDecimal(outputBlock, 0, 0, 0, false, 0);
      CreateDecimal(outputBlock, 0, 0, 0, false, 27);
      CreateDecimal(outputBlock, 0, 0, 0, true, 0);
      CreateDecimal(outputBlock, 1000000000, 0, 0, false, 0);
      CreateDecimal(outputBlock, 0, 1000000000, 0, false, 0);
      CreateDecimal(outputBlock, 0, 0, 1000000000, false, 0);
      CreateDecimal(outputBlock, 1000000000, 1000000000, 1000000000, false, 0);
      CreateDecimal(outputBlock, -1, -1, -1, false, 0);
      CreateDecimal(outputBlock, -1, -1, -1, true, 0);
      CreateDecimal(outputBlock, -1, -1, -1, false, 15);
      CreateDecimal(outputBlock, -1, -1, -1, false, 28);
      CreateDecimal(outputBlock, -1, -1, -1, false, 29);
      CreateDecimal(outputBlock, int.MaxValue, 0, 0, false, 18);
      CreateDecimal(outputBlock, int.MaxValue, 0, 0, false, 28);
      CreateDecimal(outputBlock, int.MaxValue, 0, 0, true, 28);
   }
}

/*
This example of the decimal( int, int, int, bool, byte )
constructor generates the following output.

Constructor                                               Value or Exception
-----------                                               ------------------
decimal( 0, 0, 0, False, 0 )                                               0
decimal( 0, 0, 0, False, 27 )                                              0
decimal( 0, 0, 0, True, 0 )                                                0
decimal( 1000000000, 0, 0, False, 0 )                             1000000000
decimal( 0, 1000000000, 0, False, 0 )                    4294967296000000000
decimal( 0, 0, 1000000000, False, 0 )          18446744073709551616000000000
decimal( 1000000000, 1000000000, 1000000000, False, 0 )
                                               18446744078004518913000000000
decimal( -1, -1, -1, False, 0 )                79228162514264337593543950335
decimal( -1, -1, -1, True, 0 )                -79228162514264337593543950335
decimal( -1, -1, -1, False, 15 )              79228162514264.337593543950335
decimal( -1, -1, -1, False, 28 )              7.9228162514264337593543950335
decimal( -1, -1, -1, False, 29 )                 ArgumentOutOfRangeException
decimal( 2147483647, 0, 0, False, 18 )                  0.000000002147483647
decimal( 2147483647, 0, 0, False, 28 )        0.0000000000000000002147483647
decimal( 2147483647, 0, 0, True, 28 )        -0.0000000000000000002147483647
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.