共用方式為


Decimal 建構函式

定義

初始化 Decimal 的新執行個體。

多載

Decimal(Double)

Decimal 的新執行個體初始化為指定雙精確度浮點數的值。

Decimal(Int32)

Decimal 的新執行個體初始化為指定 32 位元帶正負號整數的值。

Decimal(Int32[])

Decimal 的新執行個體初始化為以二進位表示並包含在指定陣列中的十進位值。

Decimal(Int64)

Decimal 的新執行個體初始化為指定 64 位元帶正負號整數的值。

Decimal(ReadOnlySpan<Int32>)

Decimal 的新執行個體初始化為十進位值,以二進位表示,並包含在指定範圍內。

Decimal(Single)

Decimal 的新執行個體初始化為指定單精確度浮點數的值。

Decimal(UInt32)

Decimal 的新執行個體初始化為指定 32 位元不帶正負號整數的值。

Decimal(UInt64)

Decimal 的新執行個體初始化為指定 64 位元不帶正負號整數的值。

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

從指定執行個體組成部分的參數,初始化 Decimal 的新執行個體。

Decimal(Double)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

Decimal 的新執行個體初始化為指定雙精確度浮點數的值。

public:
 Decimal(double value);
public Decimal (double value);
new decimal : double -> decimal
Public Sub New (value As Double)

參數

value
Double

要表示為 Decimal 的值。

例外狀況

value 大於 Decimal.MaxValue 或小於 Decimal.MinValue

-或-

valueNaNPositiveInfinityNegativeInfinity

範例

下列程式碼範例會使用建構函式多載建立數 DecimalDecimal 數位,以值初始化結構 Double

// Example of the Decimal( double ) constructor.
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 );
}


// Create a Decimal object and display its value.
void CreateDecimal( double value, String^ valToStr )
{
   
   // Format and display the constructor.
   Console::Write( "{0,-34}", String::Format( "Decimal( {0} )", valToStr ) );
   try
   {
      
      // Construct the Decimal value.
      Decimal decimalNum = Decimal(value);
      
      // Display the value if it was created successfully.
      Console::WriteLine( "{0,31}", decimalNum );
   }
   catch ( Exception^ ex ) 
   {
      
      // Display the exception type if an exception was thrown.
      Console::WriteLine( "{0,31}", GetExceptionType( ex ) );
   }

}

int main()
{
   Console::WriteLine( "This example of the Decimal( double ) "
   "constructor \ngenerates the following output.\n" );
   Console::WriteLine( "{0,-34}{1,31}", "Constructor", "Value or Exception" );
   Console::WriteLine( "{0,-34}{1,31}", "-----------", "------------------" );
   
   // Construct Decimal objects from double values.
   CreateDecimal( 1.23456789E+5, "1.23456789E+5" );
   CreateDecimal( 1.234567890123E+15, "1.234567890123E+15" );
   CreateDecimal( 1.2345678901234567E+25, "1.2345678901234567E+25" );
   CreateDecimal( 1.2345678901234567E+35, "1.2345678901234567E+35" );
   CreateDecimal( 1.23456789E-5, "1.23456789E-5" );
   CreateDecimal( 1.234567890123E-15, "1.234567890123E-15" );
   CreateDecimal( 1.2345678901234567E-25, "1.2345678901234567E-25" );
   CreateDecimal( 1.2345678901234567E-35, "1.2345678901234567E-35" );
   CreateDecimal( 1.0 / 7.0, "1.0 / 7.0" );
}

/*
This example of the Decimal( double ) constructor
generates the following output.

Constructor                                    Value or Exception
-----------                                    ------------------
Decimal( 1.23456789E+5 )                               123456.789
Decimal( 1.234567890123E+15 )                    1234567890123000
Decimal( 1.2345678901234567E+25 )      12345678901234600000000000
Decimal( 1.2345678901234567E+35 )               OverflowException
Decimal( 1.23456789E-5 )                          0.0000123456789
Decimal( 1.234567890123E-15 )       0.000000000000001234567890123
Decimal( 1.2345678901234567E-25 )  0.0000000000000000000000001235
Decimal( 1.2345678901234567E-35 )                               0
Decimal( 1.0 / 7.0 )                            0.142857142857143
*/
// Example of the decimal( double ) constructor.
using System;

class DecimalCtorDoDemo
{
    // 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( double value, string valToStr )
    {
        // Format and display the constructor.
        Console.Write( "{0,-34}",
            String.Format( "decimal( {0} )", valToStr ) );

        try
        {
            // Construct the decimal value.
            decimal decimalNum = new decimal( value );

            // Display the value if it was created successfully.
            Console.WriteLine( "{0,31}", decimalNum );
        }
        catch( Exception ex )
        {
            // Display the exception type if an exception was thrown.
            Console.WriteLine( "{0,31}", GetExceptionType( ex ) );
        }
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the decimal( double ) " +
            "constructor \ngenerates the following output.\n" );
        Console.WriteLine( "{0,-34}{1,31}", "Constructor",
            "Value or Exception" );
        Console.WriteLine( "{0,-34}{1,31}", "-----------",
            "------------------" );

        // Construct decimal objects from double values.
        CreateDecimal( 1.23456789E+5, "1.23456789E+5" );
        CreateDecimal( 1.234567890123E+15, "1.234567890123E+15" );
        CreateDecimal( 1.2345678901234567E+25,
            "1.2345678901234567E+25" );
        CreateDecimal( 1.2345678901234567E+35,
            "1.2345678901234567E+35" );
        CreateDecimal( 1.23456789E-5, "1.23456789E-5" );
        CreateDecimal( 1.234567890123E-15, "1.234567890123E-15" );
        CreateDecimal( 1.2345678901234567E-25,
            "1.2345678901234567E-25" );
        CreateDecimal( 1.2345678901234567E-35,
            "1.2345678901234567E-35" );
        CreateDecimal( 1.0 / 7.0, "1.0 / 7.0" );
    }
}

/*
This example of the decimal( double ) constructor
generates the following output.

Constructor                                    Value or Exception
-----------                                    ------------------
decimal( 1.23456789E+5 )                               123456.789
decimal( 1.234567890123E+15 )                    1234567890123000
decimal( 1.2345678901234567E+25 )      12345678901234600000000000
decimal( 1.2345678901234567E+35 )               OverflowException
decimal( 1.23456789E-5 )                          0.0000123456789
decimal( 1.234567890123E-15 )       0.000000000000001234567890123
decimal( 1.2345678901234567E-25 )  0.0000000000000000000000001235
decimal( 1.2345678901234567E-35 )                               0
decimal( 1.0 / 7.0 )                            0.142857142857143
*/
// Example of the decimal( double ) constructor.
open System

// Get the exception type name; remove the namespace prefix.
let getExceptionType (ex: exn) =
    let exceptionType = ex.GetType() |> string
    exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)

// Create a decimal object and display its value.
let createDecimal (value: double) valToStr =
    // Format and display the constructor.
    printf "%-34s" $"decimal( {valToStr} )"

    try
        // Construct the decimal value.
        let decimalNum = Decimal value

        // Display the value if it was created successfully.
        printfn $"{decimalNum,31}"
    with ex ->
        // Display the exception type if an exception was thrown.
        printfn $"{getExceptionType ex,31}"

printfn $"This example of the decimal( double ) constructor \ngenerates the following output.\n"
printfn "%-34s%31s" "Constructor" "Value or Exception"
printfn "%-34s%31s" "-----------" "------------------"

// Construct decimal objects from double values.
createDecimal 1.23456789E+5 "1.23456789E+5"
createDecimal 1.234567890123E+15 "1.234567890123E+15"
createDecimal 1.2345678901234567E+25 "1.2345678901234567E+25"
createDecimal 1.2345678901234567E+35 "1.2345678901234567E+35"
createDecimal 1.23456789E-5 "1.23456789E-5"
createDecimal 1.234567890123E-15 "1.234567890123E-15"
createDecimal 1.2345678901234567E-25 "1.2345678901234567E-25"
createDecimal 1.2345678901234567E-35 "1.2345678901234567E-35"
createDecimal (1. / 7.) "1. / 7."


// This example of the decimal( double ) constructor
// generates the following output.
//
// Constructor                                    Value or Exception     
// -----------                                    ------------------     
// decimal( 1.23456789E+5 )                               123456.789     
// decimal( 1.234567890123E+15 )                    1234567890123000     
// decimal( 1.2345678901234567E+25 )      12345678901234600000000000     
// decimal( 1.2345678901234567E+35 )               OverflowException     
// decimal( 1.23456789E-5 )                          0.0000123456789     
// decimal( 1.234567890123E-15 )       0.000000000000001234567890123     
// decimal( 1.2345678901234567E-25 )  0.0000000000000000000000001235     
// decimal( 1.2345678901234567E-35 )                               0
' Example of the Decimal( Double ) constructor.
Module DecimalCtorDoDemo

    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( 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( value As Double, valToStr As String )

        ' Format and display the constructor.
        Console.Write( "{0,-34}", _
            String.Format( "Decimal( {0} )", valToStr ) )

        ' Construct the Decimal value.
        Try
            Dim decimalNum As New Decimal( value )

            ' Display the value if it was created successfully.
            Console.WriteLine( "{0,31}", decimalNum )

        ' Display the exception type if an exception was thrown.
        Catch ex As Exception
            Console.WriteLine( "{0,31}", GetExceptionType( ex ) )
        End Try
    End Sub
    
    Sub Main( )

        Console.WriteLine( _
            "This example of the Decimal( Double ) constructor " & _
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-34}{1,31}", "Constructor", _
            "Value or Exception" )
        Console.WriteLine( "{0,-34}{1,31}", "-----------", _
            "------------------" )

        ' Construct Decimal objects from Double values.
        CreateDecimal( 1.23456789E+5, "1.23456789E+5" )                
        CreateDecimal( 1.234567890123E+15, "1.234567890123E+15" )                
        CreateDecimal( 1.2345678901234567E+25, _
            "1.2345678901234567E+25" )
        CreateDecimal( 1.2345678901234567E+35, _
            "1.2345678901234567E+35" )
        CreateDecimal( 1.23456789E-5, "1.23456789E-5" )                
        CreateDecimal( 1.234567890123E-15, "1.234567890123E-15" )      
        CreateDecimal( 1.2345678901234567E-25, _
            "1.2345678901234567E-25" ) 
        CreateDecimal( 1.2345678901234567E-35, _
            "1.2345678901234567E-35" ) 
        CreateDecimal( 1.0 / 7.0, "1.0 / 7.0" ) 
    End Sub 
End Module 

' This example of the Decimal( Double ) constructor
' generates the following output.
' 
' Constructor                                    Value or Exception
' -----------                                    ------------------
' Decimal( 1.23456789E+5 )                               123456.789
' Decimal( 1.234567890123E+15 )                    1234567890123000
' Decimal( 1.2345678901234567E+25 )      12345678901234600000000000
' Decimal( 1.2345678901234567E+35 )               OverflowException
' Decimal( 1.23456789E-5 )                          0.0000123456789
' Decimal( 1.234567890123E-15 )       0.000000000000001234567890123
' Decimal( 1.2345678901234567E-25 )  0.0000000000000000000000001235
' Decimal( 1.2345678901234567E-35 )                               0
' Decimal( 1.0 / 7.0 )                            0.142857142857143

備註

此建構函式會 value 使用舍入至最接近的四捨五入為 15 個有效位數。 即使數位超過 15 位數,且較不重要的數位為零,也是如此。

適用於

Decimal(Int32)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

Decimal 的新執行個體初始化為指定 32 位元帶正負號整數的值。

public:
 Decimal(int value);
public Decimal (int value);
new decimal : int -> decimal
Public Sub New (value As Integer)

參數

value
Int32

要表示為 Decimal 的值。

範例

下列程式碼範例會使用建構函式多載建立數 DecimalDecimal 數位,以使用 值初始化結構 Int32

// Example of the Decimal( int ) constructor.
using namespace System;

// Create a Decimal object and display its value.
void CreateDecimal( int value, String^ valToStr )
{
   Decimal decimalNum = Decimal(value);
   
   // Format the constructor for display.
   String^ ctor = String::Format( "Decimal( {0} )", valToStr );
   
   // Display the constructor and its value.
   Console::WriteLine( "{0,-30}{1,16}", ctor, decimalNum );
}

int main()
{
   Console::WriteLine( "This example of the Decimal( int ) "
   "constructor \ngenerates the following output.\n" );
   Console::WriteLine( "{0,-30}{1,16}", "Constructor", "Value" );
   Console::WriteLine( "{0,-30}{1,16}", "-----------", "-----" );
   
   // Construct Decimal objects from int values.
   CreateDecimal( Int32::MinValue, "Int32::MinValue" );
   CreateDecimal( Int32::MaxValue, "Int32::MaxValue" );
   CreateDecimal( 0, "0" );
   CreateDecimal( 999999999, "999999999" );
   CreateDecimal( 0x40000000, "0x40000000" );
   CreateDecimal( (int)0xC0000000, "(int)0xC0000000" );
}

/*
This example of the Decimal( int ) constructor
generates the following output.

Constructor                              Value
-----------                              -----
Decimal( Int32::MinValue )         -2147483648
Decimal( Int32::MaxValue )          2147483647
Decimal( 0 )                                 0
Decimal( 999999999 )                 999999999
Decimal( 0x40000000 )               1073741824
Decimal( (int)0xC0000000 )         -1073741824
*/
// Example of the decimal( int ) constructor.
using System;

class DecimalCtorIDemo
{
    // Create a decimal object and display its value.
    public static void CreateDecimal( int value, string valToStr )
    {
        decimal decimalNum = new decimal( value );

        // Format the constructor for display.
        string ctor = String.Format( "decimal( {0} )", valToStr );

        // Display the constructor and its value.
        Console.WriteLine( "{0,-30}{1,16}", ctor, decimalNum );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the decimal( int ) " +
            "constructor \ngenerates the following output.\n" );
        Console.WriteLine( "{0,-30}{1,16}", "Constructor", "Value" );
        Console.WriteLine( "{0,-30}{1,16}", "-----------", "-----" );

        // Construct decimal objects from int values.
        CreateDecimal( int.MinValue, "int.MinValue" );
        CreateDecimal( int.MaxValue, "int.MaxValue" );
        CreateDecimal( 0, "0" );
        CreateDecimal( 999999999, "999999999" );
        CreateDecimal( 0x40000000, "0x40000000" );
        CreateDecimal( unchecked( (int)0xC0000000 ),
            "(int)0xC0000000" );
    }
}

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

Constructor                              Value
-----------                              -----
decimal( int.MinValue )            -2147483648
decimal( int.MaxValue )             2147483647
decimal( 0 )                                 0
decimal( 999999999 )                 999999999
decimal( 0x40000000 )               1073741824
decimal( (int)0xC0000000 )         -1073741824
*/
// Example of the decimal(int) constructor.
open System

// Create a decimal object and display its value.
let createDecimal (value: int) valToStr =
    let decimalNum = new decimal(value)

    // Format the constructor for display.
    let ctor = $"decimal( {valToStr} )"

    // Display the constructor and its value.
    printfn $"{ctor,-30}{decimalNum,16}"

printfn "This example of the decimal(int) constructor\ngenerates the following output.\n"
printfn "%-30s%16s" "Constructor" "Value"
printfn "%-30s%16s" "-----------" "-----"

// Construct decimal objects from int values.
createDecimal Int32.MinValue "Int32.MinValue"
createDecimal Int32.MaxValue "int.MaxValue"
createDecimal 0 "0"
createDecimal 999999999 "999999999"
createDecimal 0x40000000 "0x40000000"
createDecimal (int 0xC0000000) "int 0xC0000000"


// This example of the decimal(int) constructor
// generates the following output.

// Constructor                              Value
// -----------                              -----
// decimal( Int32.MinValue )          -2147483648
// decimal( Int32.MaxValue )           2147483647
// decimal( 0 )                                 0
// decimal( 999999999 )                 999999999
// decimal( 0x40000000 )               1073741824
// decimal( int 0xC0000000 )          -1073741824
' Example of the Decimal( Integer ) constructor.
Module DecimalCtorIDemo

    ' Create a Decimal object and display its value.
    Sub CreateDecimal( value As Integer, valToStr As String )

        Dim decimalNum As New Decimal( value )

        ' Format the constructor for display.
        Dim ctor As String = _
            String.Format( "Decimal( {0} )", valToStr )

        ' Display the constructor and its value.
        Console.WriteLine( "{0,-33}{1,16}", ctor, decimalNum )
    End Sub
    
    Sub Main( )

        Console.WriteLine( _
            "This example of the Decimal( Integer ) constructor " & _
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-33}{1,16}", "Constructor", "Value" )
        Console.WriteLine( "{0,-33}{1,16}", "-----------", "-----" )

        ' Construct Decimal objects from Integer values.
        CreateDecimal( Integer.MinValue, "Integer.MinValue" )                
        CreateDecimal( Integer.MaxValue, "Integer.MaxValue" )                
        CreateDecimal( 0, "0" )                
        CreateDecimal( 999999999, "999999999" )                
        CreateDecimal( &H40000000, "&H40000000" )                
        CreateDecimal( &HC0000000, "&HC0000000" )                
    End Sub 
End Module 

' This example of the Decimal( Integer ) constructor
' generates the following output.
' 
' Constructor                                 Value
' -----------                                 -----
' Decimal( Integer.MinValue )           -2147483648
' Decimal( Integer.MaxValue )            2147483647
' Decimal( 0 )                                    0
' Decimal( 999999999 )                    999999999
' Decimal( &H40000000 )                  1073741824
' Decimal( &HC0000000 )                 -1073741824

適用於

Decimal(Int32[])

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

Decimal 的新執行個體初始化為以二進位表示並包含在指定陣列中的十進位值。

public:
 Decimal(cli::array <int> ^ bits);
public Decimal (int[] bits);
new decimal : int[] -> decimal
Public Sub New (bits As Integer())

參數

bits
Int32[]

32 位元帶正負號整數的陣列,包含十進位值的表示。

例外狀況

bitsnull

bits 的長度不是 4。

-或-

bits 中的十進位值表示法無效。

範例

下列程式碼範例會使用建構函式多載建立數 DecimalDecimal 數位,以四個值的陣列 Int32 初始化結構。

// Example of the Decimal( int __gc [ ] ) constructor.
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 );
}


// Create a Decimal object and display its value.
void CreateDecimal( array<int>^bits )
{
   
   // Format the constructor for display.
   String^ ctor = String::Format( "Decimal( {{ 0x{0:X}", bits[ 0 ] );
   String^ valOrExc;
   for ( int index = 1; index < bits->Length; index++ )
      ctor = String::Concat( ctor, String::Format( ", 0x{0:X}", bits[ index ] ) );
   ctor = String::Concat( ctor, " } )" );
   try
   {
      
      // Construct the Decimal value.
      Decimal decimalNum = Decimal(bits);
      
      // Format the Decimal value for display.
      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 )
      Console::WriteLine( "{0}{1}", ctor->PadRight( ctorLen ), valOrExc );
   // Otherwise, display the data on two lines.
   else
   {
      Console::WriteLine( "{0}", ctor );
      Console::WriteLine( "{0,76}", valOrExc );
   }
}

int main()
{
   Console::WriteLine( "This example of the Decimal( int __gc [ ] ) "
   "constructor \ngenerates the following output.\n" );
   Console::WriteLine( "{0,-38}{1,38}", "Constructor", "Value or Exception" );
   Console::WriteLine( "{0,-38}{1,38}", "-----------", "------------------" );
   
   // Construct Decimal objects from integer arrays.
   array<Int32>^zero = {0,0,0,0};
   CreateDecimal( zero );
   array<Int32>^arrayShort = {0,0,0};
   CreateDecimal( arrayShort );
   array<Int32>^arrayLong = {0,0,0,0,0};
   CreateDecimal( arrayLong );
   array<Int32>^word0Data = {1000000000,0,0,0};
   CreateDecimal( word0Data );
   array<Int32>^word1Data = {0,1000000000,0,0};
   CreateDecimal( word1Data );
   array<Int32>^word2Data = {0,0,1000000000,0};
   CreateDecimal( word2Data );
   array<Int32>^word3Data = {0,0,0,1000000000};
   CreateDecimal( word3Data );
   array<Int32>^decMax = { -1,-1,-1,0};
   CreateDecimal( decMax );
   array<Int32>^decMin = { -1,-1,-1,0x80000000};
   CreateDecimal( decMin );
   array<Int32>^fracDig16 = { -1,0,0,0x100000};
   CreateDecimal( fracDig16 );
   array<Int32>^fracDig28 = { -1,0,0,0x1C0000};
   CreateDecimal( fracDig28 );
   array<Int32>^fracDig29 = { -1,0,0,0x1D0000};
   CreateDecimal( fracDig29 );
   array<Int32>^reserved = { -1,0,0,0x1C0001};
   CreateDecimal( reserved );
   array<Int32>^Same4Words = {0xF0000,0xF0000,0xF0000,0xF0000};
   CreateDecimal( Same4Words );
}

/*
This example of the Decimal( int __gc [ ] ) constructor
generates the following output.

Constructor                                               Value or Exception
-----------                                               ------------------
Decimal( { 0x0, 0x0, 0x0, 0x0 } )                                          0
Decimal( { 0x0, 0x0, 0x0 } )                               ArgumentException
Decimal( { 0x0, 0x0, 0x0, 0x0, 0x0 } )                     ArgumentException
Decimal( { 0x3B9ACA00, 0x0, 0x0, 0x0 } )                          1000000000
Decimal( { 0x0, 0x3B9ACA00, 0x0, 0x0 } )                 4294967296000000000
Decimal( { 0x0, 0x0, 0x3B9ACA00, 0x0 } )       18446744073709551616000000000
Decimal( { 0x0, 0x0, 0x0, 0x3B9ACA00 } )                   ArgumentException
Decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0 } )
                                               79228162514264337593543950335
Decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000 } )
                                              -79228162514264337593543950335
Decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x100000 } )             0.0000004294967295
Decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0000 } ) 0.0000000000000000004294967295
Decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1D0000 } )              ArgumentException
Decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0001 } )              ArgumentException
Decimal( { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } )
                                                 18133887298.441562272235520
*/
// Example of the decimal( int[ ] ) constructor.
using System;

class DecimalCtorIArrDemo
{
    // 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( int[ ] bits )
    {
        // Format the constructor for display.
        string ctor = String.Format(
            "decimal( {{ 0x{0:X}", bits[ 0 ] );
        string valOrExc;

        for( int index = 1; index < bits.Length; index++ )
            ctor += String.Format( ", 0x{0:X}", bits[ index ] );
        ctor += " } )";

        try
        {
            // Construct the decimal value.
            decimal decimalNum = new decimal( bits );

            // Format the decimal value for display.
            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 )
            Console.WriteLine( "{0}{1}", ctor.PadRight( ctorLen ),
                valOrExc );

        // Otherwise, display the data on two lines.
        else
        {
            Console.WriteLine( "{0}", ctor );
            Console.WriteLine( "{0,76}", valOrExc );
        }
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the decimal( int[ ] ) constructor " +
            "\ngenerates the following output.\n" );
        Console.WriteLine( "{0,-38}{1,38}", "Constructor",
            "Value or Exception" );
        Console.WriteLine( "{0,-38}{1,38}", "-----------",
            "------------------" );

        // Construct decimal objects from integer arrays.
        CreateDecimal( new int[ ] { 0, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 1000000000, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 1000000000, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 1000000000, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0, 1000000000 } );
        CreateDecimal( new int[ ] { -1, -1, -1, 0 } );
        CreateDecimal( new int[ ]
            { -1, -1, -1, unchecked( (int)0x80000000 ) } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x100000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1C0000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1D0000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1C0001 } );
        CreateDecimal( new int[ ]
            { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } );
    }
}

/*
This example of the decimal( int[ ] ) constructor
generates the following output.

Constructor                                               Value or Exception
-----------                                               ------------------
decimal( { 0x0, 0x0, 0x0, 0x0 } )                                          0
decimal( { 0x0, 0x0, 0x0 } )                               ArgumentException
decimal( { 0x0, 0x0, 0x0, 0x0, 0x0 } )                     ArgumentException
decimal( { 0x3B9ACA00, 0x0, 0x0, 0x0 } )                          1000000000
decimal( { 0x0, 0x3B9ACA00, 0x0, 0x0 } )                 4294967296000000000
decimal( { 0x0, 0x0, 0x3B9ACA00, 0x0 } )       18446744073709551616000000000
decimal( { 0x0, 0x0, 0x0, 0x3B9ACA00 } )                   ArgumentException
decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0 } )
                                               79228162514264337593543950335
decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000 } )
                                              -79228162514264337593543950335
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x100000 } )             0.0000004294967295
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0000 } ) 0.0000000000000000004294967295
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1D0000 } )              ArgumentException
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0001 } )              ArgumentException
decimal( { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } )
                                                 18133887298.441562272235520
*/
// Example of the decimal(int[]) constructor.
open System

// Get the exception type name; remove the namespace prefix.
let getExceptionType (ex: exn) =
    let exceptionType = ex.GetType() |> string
    exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)

// Create a decimal object and display its value.
let createDecimal (bits: int[]) =
    // Format the constructor for display.
    let mutable ctor = String.Format("decimal( {{ 0x{0:X}", bits[0])

    for i = 1 to bits.Length - 1 do
        ctor <- $"{ctor}, 0x{bits[i]:X}"
    ctor <- ctor + " } )"

    let valOrExc =
        try
            // Construct the decimal value.
            let decimalNum = new decimal(bits)

            // Format the decimal value for display.
            string decimalNum
        with ex ->
            // Save the exception type if an exception was thrown.
            getExceptionType ex

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

    // Display the data on one line if it will fit.
    if ctorLen > ctor.Length then
        printfn $"{ctor.PadRight ctorLen}{valOrExc}"

    // Otherwise, display the data on two lines.
    else
        printfn $"{ctor}"
        printfn $"{valOrExc,76}"

printfn
    """This example of the decimal(int[]) constructor
generates the following output.
"""
printfn "%-38s%38s" "Constructor" "Value or Exception"
printfn "%-38s%38s" "-----------" "------------------"

// Construct decimal objects from integer arrays.
createDecimal [| 0; 0; 0; 0 |]
createDecimal [| 0; 0; 0 |]
createDecimal [| 0; 0; 0; 0; 0 |]
createDecimal [| 1000000000; 0; 0; 0 |]
createDecimal [| 0; 1000000000; 0; 0 |]
createDecimal [| 0; 0; 1000000000; 0 |]
createDecimal [| 0; 0; 0; 1000000000 |]
createDecimal [| -1; -1; -1; 0 |]
createDecimal [| -1; -1; -1; 0x80000000 |]
createDecimal [| -1; 0; 0; 0x100000 |]
createDecimal [| -1; 0; 0; 0x1C0000 |]
createDecimal [| -1; 0; 0; 0x1D0000 |]
createDecimal [| -1; 0; 0; 0x1C0001 |]
createDecimal [| 0xF0000; 0xF0000; 0xF0000; 0xF0000 |]


// This example of the decimal(int[]) constructor
// generates the following output.
//
// Constructor                                               Value or Exception
// -----------                                               ------------------
// decimal( { 0x0, 0x0, 0x0, 0x0 } )                                          0
// decimal( { 0x0, 0x0, 0x0 } )                               ArgumentException
// decimal( { 0x0, 0x0, 0x0, 0x0, 0x0 } )                     ArgumentException
// decimal( { 0x3B9ACA00, 0x0, 0x0, 0x0 } )                          1000000000
// decimal( { 0x0, 0x3B9ACA00, 0x0, 0x0 } )                 4294967296000000000
// decimal( { 0x0, 0x0, 0x3B9ACA00, 0x0 } )       18446744073709551616000000000
// decimal( { 0x0, 0x0, 0x0, 0x3B9ACA00 } )                   ArgumentException
// decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0 } )
//                                                79228162514264337593543950335
// decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000 } )
//                                               -79228162514264337593543950335
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x100000 } )             0.0000004294967295
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0000 } ) 0.0000000000000000004294967295
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1D0000 } )              ArgumentException
// decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0001 } )              ArgumentException
// decimal( { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } )
//                                                  18133887298.441562272235520
' Example of the Decimal( Integer( ) ) constructor.
Module DecimalCtorIArrDemo

    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( 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 bits( ) As Integer )

        ' Format and save the constructor.
        Dim ctor As String = String.Format( "Decimal( {{ &H{0:X}", bits( 0 ) )
        Dim valOrExc As String
        Dim index As Integer
        For index = 1 to bits.Length - 1
            ctor &= String.Format( ", &H{0:X}", bits( index ) )
        Next index
        ctor &= " } )"

        ' Construct the Decimal value.
        Try
            Dim decimalNum As New Decimal( bits )

            ' Format the Decimal value for display.
            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 As Integer = 76 - valOrExc.Length
        If ctorLen > ctor.Length Then

            ' Display the data on one line if it will fit.
            Console.WriteLine( "{0}{1}", ctor.PadRight( ctorLen ), _
                valOrExc )

        ' Otherwise, display the data on two lines.
        Else
            Console.WriteLine( "{0}", ctor )
            Console.WriteLine( "{0,76}", valOrExc )
        End If
    End Sub
    
    Sub Main( )

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

        ' Construct Decimal objects from Integer arrays.
        CreateDecimal( New Integer( ) { 0, 0, 0, 0 } )
        CreateDecimal( New Integer( ) { 0, 0, 0 } )
        CreateDecimal( New Integer( ) { 0, 0, 0, 0, 0 } )
        CreateDecimal( New Integer( ) { 1000000000, 0, 0, 0 } )
        CreateDecimal( New Integer( ) { 0, 1000000000, 0, 0 } )
        CreateDecimal( New Integer( ) { 0, 0, 1000000000, 0 } )
        CreateDecimal( New Integer( ) { 0, 0, 0, 1000000000 } )
        CreateDecimal( New Integer( ) { -1, -1, -1, 0 } )
        CreateDecimal( New Integer( ) { -1, -1, -1, &H80000000 } )
        CreateDecimal( New Integer( ) { -1, 0, 0, &H100000 } )
        CreateDecimal( New Integer( ) { -1, 0, 0, &H1C0000 } )
        CreateDecimal( New Integer( ) { -1, 0, 0, &H1D0000 } )
        CreateDecimal( New Integer( ) { -1, 0, 0, &H1C0001 } )
        CreateDecimal( New Integer( ) _
            { &HF0000, &HF0000, &HF0000, &HF0000 } )
    End Sub 
End Module 

' This example of the Decimal( Integer( ) ) constructor
' generates the following output.
' 
' Constructor                                               Value or Exception
' -----------                                               ------------------
' Decimal( { &H0, &H0, &H0, &H0 } )                                          0
' Decimal( { &H0, &H0, &H0 } )                               ArgumentException
' Decimal( { &H0, &H0, &H0, &H0, &H0 } )                     ArgumentException
' Decimal( { &H3B9ACA00, &H0, &H0, &H0 } )                          1000000000
' Decimal( { &H0, &H3B9ACA00, &H0, &H0 } )                 4294967296000000000
' Decimal( { &H0, &H0, &H3B9ACA00, &H0 } )       18446744073709551616000000000
' Decimal( { &H0, &H0, &H0, &H3B9ACA00 } )                   ArgumentException
' Decimal( { &HFFFFFFFF, &HFFFFFFFF, &HFFFFFFFF, &H0 } )
'                                                79228162514264337593543950335
' Decimal( { &HFFFFFFFF, &HFFFFFFFF, &HFFFFFFFF, &H80000000 } )
'                                               -79228162514264337593543950335
' Decimal( { &HFFFFFFFF, &H0, &H0, &H100000 } )             0.0000004294967295
' Decimal( { &HFFFFFFFF, &H0, &H0, &H1C0000 } ) 0.0000000000000000004294967295
' Decimal( { &HFFFFFFFF, &H0, &H0, &H1D0000 } )              ArgumentException
' Decimal( { &HFFFFFFFF, &H0, &H0, &H1C0001 } )              ArgumentException
' Decimal( { &HF0000, &HF0000, &HF0000, &HF0000 } )
'                                                  18133887298.441562272235520

備註

數位的 Decimal 二進位標記法包含 1 位符號、96 位整數數位,以及用來除以整數數位的縮放比例,並指定其哪個部分是小數點。 縮放比例是隱含的數位 10,會引發為範圍從 0 到 28 的指數。

bits 是 32 位帶正負號整數的四個元素長陣列。

bits [0]、 bits [1]和 bits [2] 包含 96 位整數編號的低、中間和高 32 位。

bits [3] 包含縮放比例和符號,並包含下列部分:

位 0 到 15,下一個字是未使用的,而且必須是零。

位 16 到 23 必須包含介於 0 到 28 之間的指數,這表示 10 的乘冪以除整數數。

位 24 到 30 未使用,且必須為零。

位 31 包含符號;0 表示正數,而 1 表示負數。

數值可能有數個可能的二進位標記法;全部都相同有效且數值相等。 請注意,位標記法會區分負數和正零。 這些值會在所有作業中視為相等。

另請參閱

適用於

Decimal(Int64)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

Decimal 的新執行個體初始化為指定 64 位元帶正負號整數的值。

public:
 Decimal(long value);
public Decimal (long value);
new decimal : int64 -> decimal
Public Sub New (value As Long)

參數

value
Int64

要表示為 Decimal 的值。

範例

下列程式碼範例會使用建構函式多載建立數 DecimalDecimal 數位,以使用 值初始化結構 Int64

// Example of the Decimal( __int64 ) constructor.
using namespace System;

// Create a Decimal object and display its value.
void CreateDecimal( __int64 value, String^ valToStr )
{
   Decimal decimalNum = Decimal(value);
   
   // Format the constructor for display.
   String^ ctor = String::Format( "Decimal( {0} )", valToStr );
   
   // Display the constructor and its value.
   Console::WriteLine( "{0,-35}{1,22}", ctor, decimalNum );
}

int main()
{
   Console::WriteLine( "This example of the Decimal( __int64 ) "
   "constructor \ngenerates the following output.\n" );
   Console::WriteLine( "{0,-35}{1,22}", "Constructor", "Value" );
   Console::WriteLine( "{0,-35}{1,22}", "-----------", "-----" );
   
   // Construct Decimal objects from __int64 values.
   CreateDecimal( Int64::MinValue, "Int64::MinValue" );
   CreateDecimal( Int64::MaxValue, "Int64::MaxValue" );
   CreateDecimal( 0, "0" );
   CreateDecimal( 999999999999999999, "999999999999999999" );
   CreateDecimal( 0x2000000000000000, "0x2000000000000000" );
   CreateDecimal( 0xE000000000000000, "0xE000000000000000" );
}

/*
This example of the Decimal( __int64 ) constructor
generates the following output.

Constructor                                         Value
-----------                                         -----
Decimal( Int64::MinValue )           -9223372036854775808
Decimal( Int64::MaxValue )            9223372036854775807
Decimal( 0 )                                            0
Decimal( 999999999999999999 )          999999999999999999
Decimal( 0x2000000000000000 )         2305843009213693952
Decimal( 0xE000000000000000 )        -2305843009213693952
*/
// Example of the decimal( long ) constructor.
using System;

class DecimalCtorLDemo
{
    // Create a decimal object and display its value.
    public static void CreateDecimal( long value, string valToStr )
    {
        decimal decimalNum = new decimal( value );

        // Format the constructor for display.
        string ctor = String.Format( "decimal( {0} )", valToStr );

        // Display the constructor and its value.
        Console.WriteLine( "{0,-35}{1,22}", ctor, decimalNum );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the decimal( long ) " +
            "constructor \ngenerates the following output.\n" );
        Console.WriteLine( "{0,-35}{1,22}", "Constructor", "Value" );
        Console.WriteLine( "{0,-35}{1,22}", "-----------", "-----" );

        // Construct decimal objects from long values.
        CreateDecimal( long.MinValue, "long.MinValue" );
        CreateDecimal( long.MaxValue, "long.MaxValue" );
        CreateDecimal( 0L, "0L" );
        CreateDecimal( 999999999999999999, "999999999999999999" );
        CreateDecimal( 0x2000000000000000, "0x2000000000000000" );
        CreateDecimal( unchecked( (long)0xE000000000000000 ),
            "(long)0xE000000000000000" );
    }
}

/*
This example of the decimal( long ) constructor
generates the following output.

Constructor                                         Value
-----------                                         -----
decimal( long.MinValue )             -9223372036854775808
decimal( long.MaxValue )              9223372036854775807
decimal( 0 )                                            0
decimal( 999999999999999999 )          999999999999999999
decimal( 0x2000000000000000 )         2305843009213693952
decimal( (long)0xE000000000000000 )  -2305843009213693952
*/
// Example of the decimal( int64 ) constructor.
open System

// Create a decimal object and display its value.
let createDecimal (value: int64) valToStr =
    let decimalNum = Decimal value

    // Format the constructor for display.
    let ctor = $"decimal( {valToStr} )"

    // Display the constructor and its value.
    printfn $"{ctor,-35}{decimalNum,22}"

printfn "This example of the decimal( int64 ) constructor\ngenerates the following output.\n"
printfn "%-35s%22s" "Constructor" "Value"
printfn "%-35s%22s" "-----------" "-----"

// Construct decimal objects from long values.
createDecimal Int64.MinValue "Int64.MinValue"
createDecimal Int64.MaxValue "Int64.MaxValue"
createDecimal 0L "0L"
createDecimal 999999999999999999L "999999999999999999L"
createDecimal 0x2000000000000000L "0x2000000000000000L"
createDecimal (int64 0xE000000000000000L) "int64 0xE000000000000000L"

// This example of the decimal( int64 ) constructor 
// generates the following output.
//
// Constructor                                         Value
// -----------                                         -----
// decimal( Int64.MinValue )            -9223372036854775808
// decimal( Int64.MaxValue )             9223372036854775807
// decimal( 0L )                                           0
// decimal( 999999999999999999L )         999999999999999999
// decimal( 0x2000000000000000L )        2305843009213693952
// decimal( int64 0xE000000000000000L )  -2305843009213693952
' Example of the Decimal( Long ) constructor.
Module DecimalCtorLDemo

    ' Create a Decimal object and display its value.
    Sub CreateDecimal( value As Long, valToStr As String )

        Dim decimalNum As New Decimal( value )

        ' Format the constructor for display.
        Dim ctor As String = _
            String.Format( "Decimal( {0} )", valToStr )

        ' Display the constructor and its value.
        Console.WriteLine( "{0,-30}{1,22}", ctor, decimalNum )
    End Sub
    
    Sub Main( )

        Console.WriteLine( _
            "This example of the Decimal( Long ) constructor " & _
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-30}{1,22}", "Constructor", "Value" )
        Console.WriteLine( "{0,-30}{1,22}", "-----------", "-----" )

        ' Construct Decimal objects from Long values.
        CreateDecimal( Long.MinValue, "Long.MinValue" )                
        CreateDecimal( Long.MaxValue, "Long.MaxValue" )                
        CreateDecimal( 0L, "0" )                
        CreateDecimal( 999999999999999999, "999999999999999999" )                
        CreateDecimal( &H2000000000000000, "&H2000000000000000" )                
        CreateDecimal( &HE000000000000000, "&HE000000000000000" )                
    End Sub 
End Module 

' This example of the Decimal( Long ) constructor
' generates the following output.
' 
' Constructor                                    Value
' -----------                                    -----
' Decimal( Long.MinValue )        -9223372036854775808
' Decimal( Long.MaxValue )         9223372036854775807
' Decimal( 0 )                                       0
' Decimal( 999999999999999999 )     999999999999999999
' Decimal( &H2000000000000000 )    2305843009213693952
' Decimal( &HE000000000000000 )   -2305843009213693952

適用於

Decimal(ReadOnlySpan<Int32>)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

Decimal 的新執行個體初始化為十進位值,以二進位表示,並包含在指定範圍內。

public:
 Decimal(ReadOnlySpan<int> bits);
public Decimal (ReadOnlySpan<int> bits);
new decimal : ReadOnlySpan<int> -> decimal
Public Sub New (bits As ReadOnlySpan(Of Integer))

參數

bits
ReadOnlySpan<Int32>

四個 Int32 值的範圍,其中包含十進位值的二進位表示。

例外狀況

bits 的長度不是 4,或 bits 中的十進位值表示無效。

適用於

Decimal(Single)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

Decimal 的新執行個體初始化為指定單精確度浮點數的值。

public:
 Decimal(float value);
public Decimal (float value);
new decimal : single -> decimal
Public Sub New (value As Single)

參數

value
Single

要表示為 Decimal 的值。

例外狀況

value 大於 Decimal.MaxValue 或小於 Decimal.MinValue

-或-

valueNaNPositiveInfinityNegativeInfinity

範例

下列程式碼範例會使用建構函式多載建立數 DecimalDecimal 數位,以值初始化結構 Single

// Example of the Decimal( float ) constructor.
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 );
}


// Create a Decimal object and display its value.
void CreateDecimal( float value, String^ valToStr )
{
   
   // Format and display the constructor.
   Console::Write( "{0,-27}", String::Format( "Decimal( {0} )", valToStr ) );
   try
   {
      
      // Construct the Decimal value.
      Decimal decimalNum = Decimal(value);
      
      // Display the value if it was created successfully.
      Console::WriteLine( "{0,31}", decimalNum );
   }
   catch ( Exception^ ex ) 
   {
      
      // Display the exception type if an exception was thrown.
      Console::WriteLine( "{0,31}", GetExceptionType( ex ) );
   }

}

int main()
{
   Console::WriteLine( "This example of the Decimal( float ) "
   "constructor \ngenerates the following output.\n" );
   Console::WriteLine( "{0,-27}{1,31}", "Constructor", "Value or Exception" );
   Console::WriteLine( "{0,-27}{1,31}", "-----------", "------------------" );
   
   // Construct Decimal objects from float values.
   CreateDecimal( 1.2345E+5, "1.2345E+5" );
   CreateDecimal( 1.234567E+15, "1.234567E+15" );
   CreateDecimal( 1.23456789E+25, "1.23456789E+25" );
   CreateDecimal( 1.23456789E+35, "1.23456789E+35" );
   CreateDecimal( 1.2345E-5, "1.2345E-5" );
   CreateDecimal( 1.234567E-15, "1.234567E-15" );
   CreateDecimal( 1.23456789E-25, "1.23456789E-25" );
   CreateDecimal( 1.23456789E-35, "1.23456789E-35" );
   CreateDecimal( 1.0 / 7.0, "1.0 / 7.0" );
}

/*
This example of the Decimal( float ) constructor
generates the following output.

Constructor                             Value or Exception
-----------                             ------------------
Decimal( 1.2345E+5 )                                123450
Decimal( 1.234567E+15 )                   1234567000000000
Decimal( 1.23456789E+25 )       12345680000000000000000000
Decimal( 1.23456789E+35 )                OverflowException
Decimal( 1.2345E-5 )                           0.000012345
Decimal( 1.234567E-15 )            0.000000000000001234567
Decimal( 1.23456789E-25 )   0.0000000000000000000000001235
Decimal( 1.23456789E-35 )                                0
Decimal( 1.0 / 7.0 )                             0.1428571
*/
// Example of the decimal( float ) constructor.
using System;

class DecimalCtorSDemo
{
    // 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( float value, string valToStr )
    {
        // Format and display the constructor.
        Console.Write( "{0,-27}",
            String.Format( "decimal( {0} )", valToStr ) );

        try
        {
            // Construct the decimal value.
            decimal decimalNum = new decimal( value );

            // Display the value if it was created successfully.
            Console.WriteLine( "{0,31}", decimalNum );
        }
        catch( Exception ex )
        {
            // Display the exception type if an exception was thrown.
            Console.WriteLine( "{0,31}", GetExceptionType( ex ) );
        }
    }

    public static void Main( )
    {

        Console.WriteLine( "This example of the decimal( float ) " +
            "constructor \ngenerates the following output.\n" );
        Console.WriteLine( "{0,-27}{1,31}", "Constructor",
            "Value or Exception" );
        Console.WriteLine( "{0,-27}{1,31}", "-----------",
            "------------------" );

        // Construct decimal objects from float values.
        CreateDecimal( 1.2345E+5F, "1.2345E+5F" );
        CreateDecimal( 1.234567E+15F, "1.234567E+15F" );
        CreateDecimal( 1.23456789E+25F, "1.23456789E+25F" );
        CreateDecimal( 1.23456789E+35F, "1.23456789E+35F" );
        CreateDecimal( 1.2345E-5F, "1.2345E-5F" );
        CreateDecimal( 1.234567E-15F, "1.234567E-15F" );
        CreateDecimal( 1.23456789E-25F, "1.23456789E-25F" );
        CreateDecimal( 1.23456789E-35F, "1.23456789E-35F" );
        CreateDecimal( 1.0F / 7.0F, "1.0F / 7.0F" );
    }
}

/*
This example of the decimal( float ) constructor
generates the following output.

Constructor                             Value or Exception
-----------                             ------------------
decimal( 1.2345E+5F )                               123450
decimal( 1.234567E+15F )                  1234567000000000
decimal( 1.23456789E+25F )      12345680000000000000000000
decimal( 1.23456789E+35F )               OverflowException
decimal( 1.2345E-5F )                          0.000012345
decimal( 1.234567E-15F )           0.000000000000001234567
decimal( 1.23456789E-25F )  0.0000000000000000000000001235
decimal( 1.23456789E-35F )                               0
decimal( 1.0F / 7.0F )                           0.1428571
*/
// Example of the decimal( float ) constructor.
open System

// Get the exception type name; remove the namespace prefix.
let getExceptionType (ex: exn) =
    let exceptionType = ex.GetType() |> string
    exceptionType.Substring(exceptionType.LastIndexOf '.'  + 1)

// Create a decimal object and display its value.
let createDecimal (value: float32) valToStr =
    // Format and display the constructor.
    printf "%-27s" $"decimal( {valToStr} )"

    try
        // Construct the decimal value.
        let decimalNum = Decimal value

        // Display the value if it was created successfully.
        printfn $"{decimalNum,31}"
    with ex ->
        // Display the exception type if an exception was thrown.
        printfn $"{getExceptionType ex,31}"


printfn "This example of the decimal( float ) constructor \ngenerates the following output.\n"
printfn $"""{"Constructor",-27}{"Value or Exception",31}"""
printfn $"""{"-----------",-27}{"------------------",31}"""

// Construct decimal objects from float values.
createDecimal 1.2345E+5f "1.2345E+5F"
createDecimal 1.234567E+15f "1.234567E+15F"
createDecimal 1.23456789E+25f "1.23456789E+25F"
createDecimal 1.23456789E+35f "1.23456789E+35F"
createDecimal 1.2345E-5f "1.2345E-5F"
createDecimal 1.234567E-15f "1.234567E-15F"
createDecimal 1.23456789E-25f "1.23456789E-25F"
createDecimal 1.23456789E-35f "1.23456789E-35F"
createDecimal (1f / 7f) "1.0F / 7.0F"


// This example of the decimal( float ) constructor
// generates the following output.

// Constructor                             Value or Exception
// -----------                             ------------------
// decimal( 1.2345E+5F )                               123450
// decimal( 1.234567E+15F )                  1234567000000000
// decimal( 1.23456789E+25F )      12345680000000000000000000
// decimal( 1.23456789E+35F )               OverflowException
// decimal( 1.2345E-5F )                          0.000012345
// decimal( 1.234567E-15F )           0.000000000000001234567
// decimal( 1.23456789E-25F )  0.0000000000000000000000001235
// decimal( 1.23456789E-35F )                               0
// decimal( 1.0F / 7.0F )                           0.1428571
' Example of the Decimal( Single ) constructor.
Module DecimalCtorSDemo

    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( 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( value As Single, valToStr As String )

        ' Format and display the constructor.
        Console.Write( "{0,-27}", _
            String.Format( "Decimal( {0} )", valToStr ) )

        ' Construct the Decimal value.
        Try
            Dim decimalNum As New Decimal( value )

            ' Display the value if it was created successfully.
            Console.WriteLine( "{0,31}", decimalNum )

        ' Display the exception type if an exception was thrown.
        Catch ex As Exception
            Console.WriteLine( "{0,31}", GetExceptionType( ex ) )
        End Try
    End Sub
    
    Sub Main( )

        Console.WriteLine( _
            "This example of the Decimal( Single ) constructor " & _
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-27}{1,31}", "Constructor", "Value or Exception" )
        Console.WriteLine( "{0,-27}{1,31}", "-----------", "------------------" )

        ' Construct Decimal objects from Single values.
        CreateDecimal( 1.2345E+5, "1.2345E+5" )                
        CreateDecimal( 1.234567E+15, "1.234567E+15" )                
        CreateDecimal( 1.23456789E+25, "1.23456789E+25" )                
        CreateDecimal( 1.23456789E+35, "1.23456789E+35" )                
        CreateDecimal( 1.2345E-5, "1.2345E-5" )                
        CreateDecimal( 1.234567E-15, "1.234567E-15" )                
        CreateDecimal( 1.23456789E-25, "1.23456789E-25" )                
        CreateDecimal( 1.23456789E-35, "1.23456789E-35" )                
        CreateDecimal( 1.0 / 7.0, "1.0 / 7.0" )                
    End Sub 
End Module 

' This example of the Decimal( Single ) constructor
' generates the following output.
' 
' Constructor                             Value or Exception
' -----------                             ------------------
' Decimal( 1.2345E+5 )                                123450
' Decimal( 1.234567E+15 )                   1234567000000000
' Decimal( 1.23456789E+25 )       12345680000000000000000000
' Decimal( 1.23456789E+35 )                OverflowException
' Decimal( 1.2345E-5 )                           0.000012345
' Decimal( 1.234567E-15 )            0.000000000000001234567
' Decimal( 1.23456789E-25 )   0.0000000000000000000000001235
' Decimal( 1.23456789E-35 )                                0
' Decimal( 1.0 / 7.0 )                             0.1428571

備註

此建構函式會 value 使用舍入至最接近的四捨五入四捨五入至 7 個有效位數。 即使數位超過 7 位數,且較不重要的數位為零,也是如此。

適用於

Decimal(UInt32)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

重要

此 API 不符合 CLS 規範。

Decimal 的新執行個體初始化為指定 32 位元不帶正負號整數的值。

public:
 Decimal(System::UInt32 value);
[System.CLSCompliant(false)]
public Decimal (uint value);
[<System.CLSCompliant(false)>]
new decimal : uint32 -> decimal
Public Sub New (value As UInteger)

參數

value
UInt32

要表示為 Decimal 的值。

屬性

範例

下列程式碼範例會使用建構函式多載建立數 DecimalDecimal 數位,以值初始化結構 UInt32

// Example of the Decimal( unsigned int ) constructor.
using namespace System;

// Create a Decimal object and display its value.
void CreateDecimal( unsigned int value, String^ valToStr )
{
   Decimal decimalNum = Decimal(value);
   
   // Format the constructor for display.
   String^ ctor = String::Format( "Decimal( {0} )", valToStr );
   
   // Display the constructor and its value.
   Console::WriteLine( "{0,-30}{1,16}", ctor, decimalNum );
}

int main()
{
   Console::WriteLine( "This example of the Decimal( unsigned "
   "int ) constructor \ngenerates the following output.\n" );
   Console::WriteLine( "{0,-30}{1,16}", "Constructor", "Value" );
   Console::WriteLine( "{0,-30}{1,16}", "-----------", "-----" );
   
   // Construct Decimal objects from unsigned int values.
   CreateDecimal( UInt32::MinValue, "UInt32::MinValue" );
   CreateDecimal( UInt32::MaxValue, "UInt32::MaxValue" );
   CreateDecimal( Int32::MaxValue, "Int32::MaxValue" );
   CreateDecimal( 999999999, "999999999" );
   CreateDecimal( 0x40000000, "0x40000000" );
   CreateDecimal( 0xC0000000, "0xC0000000" );
}

/*
This example of the Decimal( unsigned int ) constructor
generates the following output.

Constructor                              Value
-----------                              -----
Decimal( UInt32::MinValue )                  0
Decimal( UInt32::MaxValue )         4294967295
Decimal( Int32::MaxValue )          2147483647
Decimal( 999999999 )                 999999999
Decimal( 0x40000000 )               1073741824
Decimal( 0xC0000000 )               3221225472
*/
// Example of the decimal( uint ) constructor.
using System;

class DecimalCtorUIDemo
{
    // Create a decimal object and display its value.
    public static void CreateDecimal( uint value, string valToStr )
    {
        decimal decimalNum = new decimal( value );

        // Format the constructor for display.
        string ctor = String.Format( "decimal( {0} )", valToStr );

        // Display the constructor and its value.
        Console.WriteLine( "{0,-33}{1,16}", ctor, decimalNum );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the decimal( uint ) " +
            "constructor \ngenerates the following output.\n" );
        Console.WriteLine( "{0,-33}{1,16}", "Constructor", "Value" );
        Console.WriteLine( "{0,-33}{1,16}", "-----------", "-----" );

        // Construct decimal objects from uint values.
        CreateDecimal( uint.MinValue, "uint.MinValue" );
        CreateDecimal( uint.MaxValue, "uint.MaxValue" );
        CreateDecimal( (uint)int.MaxValue, "(uint)int.MaxValue" );
        CreateDecimal( 999999999U, "999999999U" );
        CreateDecimal( 0x40000000U, "0x40000000U" );
        CreateDecimal( 0xC0000000, "0xC0000000" );
    }
}

/*
This example of the decimal( uint ) constructor
generates the following output.

Constructor                                 Value
-----------                                 -----
decimal( uint.MinValue )                        0
decimal( uint.MaxValue )               4294967295
decimal( (uint)int.MaxValue )          2147483647
decimal( 999999999U )                   999999999
decimal( 0x40000000U )                 1073741824
decimal( 0xC0000000 )                  3221225472
*/
// Example of the decimal( uint ) constructor.
open System

// Create a decimal object and display its value.
let createDecimal (value: uint) valToStr =
    let decimalNum = Decimal value

    // Format the constructor for display.
    let ctor = $"decimal( {valToStr} )"

    // Display the constructor and its value.
    printfn $"{ctor,-33}{decimalNum,16}"

printfn "This example of the decimal( uint ) constructor \ngenerates the following output.\n"
printfn "%-33s%16s" "Constructor" "Value"
printfn "%-33s%16s" "-----------" "-----" 

// Construct decimal objects from uint values.
createDecimal UInt32.MinValue "UInt32.MinValue"
createDecimal UInt32.MaxValue "UInt32.MaxValue"
createDecimal (uint Int32.MaxValue) "uint Int32.MaxValue"
createDecimal 999999999u "999999999u"
createDecimal 0x40000000u "0x40000000u"
createDecimal 0xC0000000u "0xC0000000u"


// This example of the decimal( uint ) constructor 
// generates the following output.
//
// Constructor                                 Value
// -----------                                 -----
// decimal( UInt32.MinValue )                      0
// decimal( UInt32.MaxValue )             4294967295
// decimal( uint Int32.MaxValue )         2147483647
// decimal( 999999999u )                   999999999
// decimal( 0x40000000u )                 1073741824
// decimal( 0xC0000000u )                 3221225472
' Example of the Decimal( UInt32 ) constructor.
Module DecimalCtorUIDemo

    ' Create a Decimal object and display its value.
    Sub CreateDecimal( value As UInt32, valToStr As String )

        Dim decimalNum As New Decimal( value )

        ' Format the constructor for display.
        Dim ctor As String = _
            String.Format( "Decimal( {0} )", valToStr )

        ' Display the constructor and its value.
        Console.WriteLine( "{0,-33}{1,16}", ctor, decimalNum )
    End Sub
    
    Sub Main( )

        Console.WriteLine( _
            "This example of the Decimal( UInt32 ) constructor " & _
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-33}{1,16}", "Constructor", "Value" )
        Console.WriteLine( "{0,-33}{1,16}", "-----------", "-----" )

        ' Construct Decimal objects from UInt32 values.
        ' UInt32.MinValue and UInt32.MaxValue are not defined in VB.
        CreateDecimal( Convert.ToUInt32( 0 ), """UInt32.MinValue""" )
        CreateDecimal( Convert.ToUInt32( 4294967295 ), _
            """UInt32.MaxValue""" )
        CreateDecimal( Convert.ToUInt32( Integer.MaxValue ), _
            "Integer.MaxValue" )              
        CreateDecimal( Convert.ToUInt32( 999999999 ), "999999999" ) 
        CreateDecimal( Convert.ToUInt32( &H40000000 ), "&H40000000" ) 
        CreateDecimal( Convert.ToUInt32( &HC0000000L ), "&HC0000000" )
    End Sub 
End Module 

' This example of the Decimal( UInt32 ) constructor
' generates the following output.
' 
' Constructor                                 Value
' -----------                                 -----
' Decimal( "UInt32.MinValue" )                    0
' Decimal( "UInt32.MaxValue" )           4294967295
' Decimal( Integer.MaxValue )            2147483647
' Decimal( 999999999 )                    999999999
' Decimal( &H40000000 )                  1073741824
' Decimal( &HC0000000 )                  3221225472

適用於

Decimal(UInt64)

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

重要

此 API 不符合 CLS 規範。

Decimal 的新執行個體初始化為指定 64 位元不帶正負號整數的值。

public:
 Decimal(System::UInt64 value);
[System.CLSCompliant(false)]
public Decimal (ulong value);
[<System.CLSCompliant(false)>]
new decimal : uint64 -> decimal
Public Sub New (value As ULong)

參數

value
UInt64

要表示為 Decimal 的值。

屬性

範例

下列程式碼範例會使用建構函式多載建立數 DecimalDecimal 數位,以值初始化結構 UInt64

// Example of the Decimal( unsigned __int64 ) constructor.
using namespace System;

// Create a Decimal object and display its value.
void CreateDecimal( unsigned __int64 value, String^ valToStr )
{
   Decimal decimalNum = Decimal(value);
   
   // Format the constructor for display.
   String^ ctor = String::Format( "Decimal( {0} )", valToStr );
   
   // Display the constructor and its value.
   Console::WriteLine( "{0,-33}{1,22}", ctor, decimalNum );
}

int main()
{
   Console::WriteLine( "This example of the Decimal( unsigned "
   "__int64 ) constructor \ngenerates the following output.\n" );
   Console::WriteLine( "{0,-33}{1,22}", "Constructor", "Value" );
   Console::WriteLine( "{0,-33}{1,22}", "-----------", "-----" );
   
   // Construct Decimal objects from unsigned __int64 values.
   CreateDecimal( UInt64::MinValue, "UInt64::MinValue" );
   CreateDecimal( UInt64::MaxValue, "UInt64::MaxValue" );
   CreateDecimal( Int64::MaxValue, "Int64::MaxValue" );
   CreateDecimal( 999999999999999999, "999999999999999999" );
   CreateDecimal( 0x2000000000000000, "0x2000000000000000" );
   CreateDecimal( 0xE000000000000000, "0xE000000000000000" );
}

/*
This example of the Decimal( unsigned __int64 ) constructor
generates the following output.

Constructor                                       Value
-----------                                       -----
Decimal( UInt64::MinValue )                           0
Decimal( UInt64::MaxValue )        18446744073709551615
Decimal( Int64::MaxValue )          9223372036854775807
Decimal( 999999999999999999 )        999999999999999999
Decimal( 0x2000000000000000 )       2305843009213693952
Decimal( 0xE000000000000000 )      16140901064495857664
*/
// Example of the decimal( ulong ) constructor.
using System;

class DecimalCtorLDemo
{
    // Create a decimal object and display its value.
    public static void CreateDecimal( ulong value, string valToStr )
    {
        decimal decimalNum = new decimal( value );

        // Format the constructor for display.
        string ctor = String.Format( "decimal( {0} )", valToStr );

        // Display the constructor and its value.
        Console.WriteLine( "{0,-35}{1,22}", ctor, decimalNum );
    }

    public static void Main( )
    {
        Console.WriteLine( "This example of the decimal( ulong ) " +
            "constructor \ngenerates the following output.\n" );
        Console.WriteLine( "{0,-35}{1,22}", "Constructor", "Value" );
        Console.WriteLine( "{0,-35}{1,22}", "-----------", "-----" );

        // Construct decimal objects from ulong values.
        CreateDecimal( ulong.MinValue, "ulong.MinValue" );
        CreateDecimal( ulong.MaxValue, "ulong.MaxValue" );
        CreateDecimal( long.MaxValue, "long.MaxValue" );
        CreateDecimal( 999999999999999999, "999999999999999999" );
        CreateDecimal( 0x2000000000000000, "0x2000000000000000" );
        CreateDecimal( 0xE000000000000000, "0xE000000000000000" );
    }
}

/*
This example of the decimal( ulong ) constructor
generates the following output.

Constructor                                         Value
-----------                                         -----
decimal( ulong.MinValue )                               0
decimal( ulong.MaxValue )            18446744073709551615
decimal( long.MaxValue )              9223372036854775807
decimal( 999999999999999999 )          999999999999999999
decimal( 0x2000000000000000 )         2305843009213693952
decimal( 0xE000000000000000 )        16140901064495857664
*/
// Example of the decimal( uint64 ) constructor.
open System

// Create a decimal object and display its value.
let createDecimal (value: uint64) valToStr =
    let decimalNum = Decimal value

    // Format the constructor for display.
    let ctor = $"decimal( {valToStr} )"

    // Display the constructor and its value.
    printfn $"{ctor,-35}{decimalNum,22}"

printfn "This example of the decimal( uint64 ) constructor \ngenerates the following output.\n"
printfn "%-35s%22s" "Constructor" "Value"
printfn "%-35s%22s" "-----------" "-----"

// Construct decimal objects from ulong values.
createDecimal UInt64.MinValue "UInt64.MinValue"
createDecimal UInt64.MaxValue "UInt64.MaxValue"
createDecimal (uint64 Int64.MaxValue) "int64 Int64.MaxValue"
createDecimal 999999999999999999uL "999999999999999999uL"
createDecimal 0x2000000000000000uL "0x2000000000000000uL"
createDecimal 0xE000000000000000uL "0xE000000000000000uL"


// This example of the decimal( uint64 ) constructor 
// generates the following output.
//
// Constructor                                         Value
// -----------                                         -----
// decimal( UInt64.MinValue )                              0
// decimal( UInt64.MaxValue )           18446744073709551615
// decimal( int64 Int64.MaxValue )       9223372036854775807
// decimal( 999999999999999999uL )        999999999999999999
// decimal( 0x2000000000000000uL )       2305843009213693952
// decimal( 0xE000000000000000uL )      16140901064495857664
' Example of the Decimal( UInt64 ) constructor.
Module DecimalCtorULDemo

    ' Create a Decimal object and display its value.
    Sub CreateDecimal( value As UInt64, valToStr As String )

        Dim decimalNum As New Decimal( value )

        ' Format the constructor for display.
        Dim ctor As String = _
            String.Format( "Decimal( {0} )", valToStr )

        ' Display the constructor and its value.
        Console.WriteLine( "{0,-33}{1,22}", ctor, decimalNum )
    End Sub
    
    Sub Main( )

        Console.WriteLine( _
            "This example of the Decimal( UInt64 ) constructor " & _
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-33}{1,22}", "Constructor", "Value" )
        Console.WriteLine( "{0,-33}{1,22}", "-----------", "-----" )

        ' Construct Decimal objects from UInt64 values.
        ' UInt64.MinValue and UInt64.MaxValue are not defined in VB.
        CreateDecimal( Convert.ToUInt64( 0 ), """UInt64.MinValue""" )
        CreateDecimal( Convert.ToUInt64( 18446744073709551615D ), _
            """UInt64.MaxValue""" )
        CreateDecimal( Convert.ToUInt64( Long.MaxValue ), _
            "Long.MaxValue" )              
        CreateDecimal( Convert.ToUInt64( 999999999999999999 ), _
            "999999999999999999" )                
        CreateDecimal( Convert.ToUInt64( &H2000000000000000 ), _
            "&H2000000000000000" )                
        CreateDecimal( Convert.ToUInt64( 16140901064495857664.0 ), _
            "16140901064495857664.0" )                
    End Sub 
End Module 

' This example of the Decimal( UInt64 ) constructor
' generates the following output.
' 
' Constructor                                       Value
' -----------                                       -----
' Decimal( "UInt64.MinValue" )                          0
' Decimal( "UInt64.MaxValue" )       18446744073709551615
' Decimal( Long.MaxValue )            9223372036854775807
' Decimal( 999999999999999999 )        999999999999999999
' Decimal( &H2000000000000000 )       2305843009213693952
' Decimal( 16140901064495857664.0 )  16140901064495857664

適用於

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

來源:
Decimal.cs
來源:
Decimal.cs
來源:
Decimal.cs

從指定執行個體組成部分的參數,初始化 Decimal 的新執行個體。

public:
 Decimal(int lo, int mid, int hi, bool isNegative, System::Byte scale);
public Decimal (int lo, int mid, int hi, bool isNegative, byte scale);
new decimal : int * int * int * bool * byte -> decimal
Public Sub New (lo As Integer, mid As Integer, hi As Integer, isNegative As Boolean, scale As Byte)

參數

lo
Int32

96 位元整數的低 32 位元。

mid
Int32

96 位元整數的中 32 位元。

hi
Int32

96 位元整數的高 32 位元。

isNegative
Boolean

true 表示負數,false 表示正數。

scale
Byte

範圍從 0 到 28 的 10 乘冪。

例外狀況

scale 大於 28。

範例

下列程式碼範例會使用建構函式多載建立數 Decimal 個數字,以三 Int32Decimal 值字組、 Boolean 符號和 Byte 小數位數來初始化結構。

// Example of the Decimal( int, int, int, bool, unsigned char ) 
// constructor.
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 );
}


// Create a Decimal object and display its value.
void CreateDecimal( int low, int mid, int high, bool isNeg, unsigned char scale )
{
   
   // Format the constructor for display.
   array<Object^>^boxedParams = gcnew array<Object^>(5);
   boxedParams[ 0 ] = low;
   boxedParams[ 1 ] = mid;
   boxedParams[ 2 ] = high;
   boxedParams[ 3 ] = isNeg;
   boxedParams[ 4 ] = scale;
   String^ ctor = String::Format( "Decimal( {0}, {1}, {2}, {3}, {4} )", boxedParams );
   String^ valOrExc;
   try
   {
      
      // Construct the Decimal value.
      Decimal decimalNum = 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 )
      Console::WriteLine( "{0}{1}", ctor->PadRight( ctorLen ), valOrExc );
   // Otherwise, display the data on two lines.
   else
   {
      Console::WriteLine( "{0}", ctor );
      Console::WriteLine( "{0,76}", valOrExc );
   }
}

int main()
{
   Console::WriteLine( "This example of the Decimal( int, int, "
   "int, bool, unsigned char ) \nconstructor "
   "generates the following output.\n" );
   Console::WriteLine( "{0,-38}{1,38}", "Constructor", "Value or Exception" );
   Console::WriteLine( "{0,-38}{1,38}", "-----------", "------------------" );
   
   // Construct Decimal objects from double values.
   CreateDecimal( 0, 0, 0, false, 0 );
   CreateDecimal( 0, 0, 0, false, 27 );
   CreateDecimal( 0, 0, 0, true, 0 );
   CreateDecimal( 1000000000, 0, 0, false, 0 );
   CreateDecimal( 0, 1000000000, 0, false, 0 );
   CreateDecimal( 0, 0, 1000000000, false, 0 );
   CreateDecimal( 1000000000, 1000000000, 1000000000, false, 0 );
   CreateDecimal(  -1, -1, -1, false, 0 );
   CreateDecimal(  -1, -1, -1, true, 0 );
   CreateDecimal(  -1, -1, -1, false, 15 );
   CreateDecimal(  -1, -1, -1, false, 28 );
   CreateDecimal(  -1, -1, -1, false, 29 );
   CreateDecimal( Int32::MaxValue, 0, 0, false, 18 );
   CreateDecimal( Int32::MaxValue, 0, 0, false, 28 );
   CreateDecimal( Int32::MaxValue, 0, 0, true, 28 );
}

/*
This example of the Decimal( int, int, int, bool, unsigned char )
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 DecimalCtorIIIBByDemo
{
    // 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( 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 )
            Console.WriteLine( "{0}{1}", ctor.PadRight( ctorLen ),
                valOrExc );

        // Otherwise, display the data on two lines.
        else
        {
            Console.WriteLine( "{0}", ctor );
            Console.WriteLine( "{0,76}", valOrExc );
        }
    }

    public static void Main( )
    {

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

        // Construct decimal objects from the component fields.
        CreateDecimal( 0, 0, 0, false, 0 );
        CreateDecimal( 0, 0, 0, false, 27 );
        CreateDecimal( 0, 0, 0, true, 0 );
        CreateDecimal( 1000000000, 0, 0, false, 0 );
        CreateDecimal( 0, 1000000000, 0, false, 0 );
        CreateDecimal( 0, 0, 1000000000, false, 0 );
        CreateDecimal( 1000000000, 1000000000, 1000000000, false, 0 );
        CreateDecimal( -1, -1, -1, false, 0 );
        CreateDecimal( -1, -1, -1, true, 0 );
        CreateDecimal( -1, -1, -1, false, 15 );
        CreateDecimal( -1, -1, -1, false, 28 );
        CreateDecimal( -1, -1, -1, false, 29 );
        CreateDecimal( int.MaxValue, 0, 0, false, 18 );
        CreateDecimal( int.MaxValue, 0, 0, false, 28 );
        CreateDecimal( 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
*/
// Example of the decimal( int, int, int, bool, byte ) constructor.
open System

// Get the exception type name; remove the namespace prefix.
let getExceptionType (ex: exn) =
    let exceptionType = ex.GetType() |> string
    exceptionType.Substring(exceptionType.LastIndexOf '.' + 1)

// Create a decimal object and display its value.
let createDecimal low mid high (isNeg: bool) (scale: byte) =
    // Format the constructor for display.
    let ctor =
        $"decimal( %i{low}, %i{mid}, %i{high}, {isNeg}, {scale} )"

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

            // Format and save the decimal value.
            decimalNum |> string
        with ex ->
            // Save the exception type if an exception was thrown.
            getExceptionType ex

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

    // Display the data on one line if it will fit.
    if ctorLen > ctor.Length then
        printfn $"{ctor.PadRight ctorLen}{valOrExc}"

    // Otherwise, display the data on two lines.
    else
        printfn $"{ctor}"
        printfn $"{valOrExc,76}"

printfn 
    """This example of the decimal(int, int, int, bool, byte) 
constructor generates the following output.
"""
printfn "%-38s%38s" "Constructor" "Value or Exception"
printfn "%-38s%38s" "-----------" "------------------"

// Construct decimal objects from the component fields.
createDecimal 0 0 0 false 0uy
createDecimal 0 0 0 false 27uy
createDecimal 0 0 0 true 0uy
createDecimal 1000000000 0 0 false 0uy
createDecimal 0 1000000000 0 false 0uy
createDecimal 0 0 1000000000 false 0uy
createDecimal 1000000000 1000000000 1000000000 false 0uy
createDecimal -1 -1 -1 false 0uy
createDecimal -1 -1 -1 true 0uy
createDecimal -1 -1 -1 false 15uy
createDecimal -1 -1 -1 false 28uy
createDecimal -1 -1 -1 false 29uy
createDecimal Int32.MaxValue 0 0 false 18uy
createDecimal Int32.MaxValue 0 0 false 28uy
createDecimal Int32.MaxValue 0 0 true 28uy


// 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
' Example of the Decimal( Integer, Integer, Integer, Boolean, Byte ) 
' constructor.
Module DecimalCtorIIIBByDemo

    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( 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( low As Integer, mid As Integer, _
        high As Integer, isNeg As Boolean, 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.
            Console.WriteLine( "{0}{1}", ctor.PadRight( ctorLen ), _
                valOrExc )

        ' Otherwise, display the data on two lines.
        Else
            Console.WriteLine( "{0}", ctor )
            Console.WriteLine( "{0,76}", valOrExc )
        End If
    End Sub
    
    Sub Main( )

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

        ' Construct Decimal objects from the component fields.
        CreateDecimal( 0, 0, 0, False, 0 )                
        CreateDecimal( 0, 0, 0, False, 27 )                
        CreateDecimal( 0, 0, 0, True, 0 )                
        CreateDecimal( 1000000000, 0, 0, False, 0 )                
        CreateDecimal( 0, 1000000000, 0, False, 0 )                
        CreateDecimal( 0, 0, 1000000000, False, 0 )                
        CreateDecimal( 1000000000, 1000000000, 1000000000, False, 0 )
        CreateDecimal( -1, -1, -1, False, 0 )                
        CreateDecimal( -1, -1, -1, True, 0 )                
        CreateDecimal( -1, -1, -1, False, 15 )                
        CreateDecimal( -1, -1, -1, False, 28 )                
        CreateDecimal( -1, -1, -1, False, 29 )                
        CreateDecimal( Integer.MaxValue, 0, 0, False, 18 )                
        CreateDecimal( Integer.MaxValue, 0, 0, False, 28 )                
        CreateDecimal( 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

下列範例會 GetBits 使用 方法來擷取陣列的元件部分。 然後,它會在呼叫建 Decimal(Int32, Int32, Int32, Boolean, Byte) 構函式時使用此陣列來具現化新的 Decimal 值。

using System;

public class Example
{
   public static void Main()
   {
      Decimal[] values = { 1234.96m, -1234.96m };
      foreach (var value in values) {
         int[] parts = Decimal.GetBits(value);
         bool sign = (parts[3] & 0x80000000) != 0;

         byte scale = (byte) ((parts[3] >> 16) & 0x7F);
         Decimal newValue = new Decimal(parts[0], parts[1], parts[2], sign, scale);
         Console.WriteLine("{0} --> {1}", value, newValue);
      }
   }
}
// The example displays the following output:
//       1234.96 --> 1234.96
//       -1234.96 --> -1234.96
open System

let values = [ 1234.96m; -1234.96m ]
for value in values do
    let parts = Decimal.GetBits value
    let sign = (parts[3] &&& 0x80000000) <> 0

    let scale = (parts[3] >>> 16) &&& 0x7F |> byte
    let newValue = Decimal(parts[0], parts[1], parts[2], sign, scale)
    printfn $"{value} --> {newValue}"

// The example displays the following output:
//       1234.96 --> 1234.96
//       -1234.96 --> -1234.96
Module Example
   Public Sub Main()
      Dim values() As Decimal = { 1234.96d, -1234.96d }
      For Each value In values
         Dim parts() = Decimal.GetBits(value)
         Dim sign As Boolean = (parts(3) And &h80000000) <> 0
         Dim scale As Byte = CByte((parts(3) >> 16) And &H7F)
   
         Dim newValue As New Decimal(parts(0), parts(1), parts(2), sign, scale)    
         Console.WriteLine("{0} --> {1}", value, newValue)
      Next   
   End Sub
End Module
' The example displays the following output:
'    1234.96 --> 1234.96
'    -1234.96 --> -1234.96

備註

數位的 Decimal 二進位標記法包含 1 位符號、96 位整數數位,以及用來除以整數數位的縮放比例,並指定其哪個部分是小數點。 縮放比例是隱含地將數位 10 引發至 0 到 28 的指數。

適用於