Auf Englisch lesen

Freigeben über


Decimal Konstruktoren

Definition

Initialisiert eine neue Instanz von Decimal.

Überlädt

Decimal(Double)

Initialisiert eine neue Instanz von Decimal mit dem Wert der angegebenen Gleitkommazahl mit doppelter Genauigkeit.

Decimal(Int32)

Initialisiert eine neue Instanz von Decimal mit dem Wert einer angegebenen 32-Bit-Ganzzahl mit Vorzeichen.

Decimal(Int32[])

Initialisiert eine neue Instanz von Decimal mit einem binär dargestellten Decimal-Wert in einem angegebenen Array.

Decimal(Int64)

Initialisiert eine neue Instanz von Decimal mit dem Wert einer angegebenen 64-Bit-Ganzzahl mit Vorzeichen.

Decimal(ReadOnlySpan<Int32>)

Initialisiert eine neue instance von Decimal zu einem Dezimalwert, der in binär dargestellt und in der angegebenen Spanne enthalten ist.

Decimal(Single)

Initialisiert eine neue Instanz von Decimal mit dem Wert der angegebenen Gleitkommazahl mit einfacher Genauigkeit.

Decimal(UInt32)

Initialisiert eine neue Instanz von Decimal mit dem Wert einer angegebenen 32-Bit-Ganzzahl ohne Vorzeichen.

Decimal(UInt64)

Initialisiert eine neue Instanz von Decimal mit dem Wert einer angegebenen 64-Bit-Ganzzahl ohne Vorzeichen.

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

Initialisiert einen neue Instanz von Decimal mit Parametern, die die Bestandteile der Instanz angeben.

Decimal(Double)

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Initialisiert eine neue Instanz von Decimal mit dem Wert der angegebenen Gleitkommazahl mit doppelter Genauigkeit.

C#
public Decimal (double value);

Parameter

value
Double

Der als Decimal darzustellende Wert.

Ausnahmen

value ist größer als Decimal.MaxValue oder kleiner als Decimal.MinValue.

- oder -

value ist NaN, PositiveInfinity oder NegativeInfinity.

Beispiele

Im folgenden Codebeispiel werden mehrere Decimal Zahlen mithilfe der Konstruktorüberladung erstellt, die eine Decimal Struktur mit einem Double Wert initialisiert.

C#
// 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
*/

Hinweise

Dieser Konstruktor rundet value mithilfe von Rundung auf nächstliegende Ziffern auf 15 wichtige Ziffern ab. Dies geschieht auch dann, wenn die Zahl mehr als 15 Ziffern aufweist und die weniger signifikanten Ziffern 0 sind.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Decimal(Int32)

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Initialisiert eine neue Instanz von Decimal mit dem Wert einer angegebenen 32-Bit-Ganzzahl mit Vorzeichen.

C#
public Decimal (int value);

Parameter

value
Int32

Der als Decimal darzustellende Wert.

Beispiele

Im folgenden Codebeispiel werden mehrere Decimal Zahlen mithilfe der Konstruktorüberladung erstellt, die eine Decimal Struktur mit einem Int32 Wert initialisiert.

C#
// 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
*/

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Decimal(Int32[])

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Initialisiert eine neue Instanz von Decimal mit einem binär dargestellten Decimal-Wert in einem angegebenen Array.

C#
public Decimal (int[] bits);

Parameter

bits
Int32[]

Eine Array von 32-Bit-Ganzzahlen mit Vorzeichen, das eine Darstellung eines Decimal-Werts enthält.

Ausnahmen

bits ist null.

Die Länge von bits ist nicht 4.

- oder -

Die Darstellung des Decimal-Werts als bits ist ungültig.

Beispiele

Im folgenden Codebeispiel werden mehrere Decimal Zahlen mithilfe der Konstruktorüberladung erstellt, die eine Decimal Struktur mit einem Array von vier Int32 Werten initialisiert.

C#
// 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
*/

Hinweise

Die binäre Darstellung einer Decimal Zahl besteht aus einem 1-Bit-Vorzeichen, einer 96-Bit-Ganzzahl und einem Skalierungsfaktor, der verwendet wird, um die ganzzahlige Zahl zu dividieren und anzugeben, welcher Teil davon ein Dezimalbruch ist. Der Skalierungsfaktor ist implizit die Zahl 10, die auf einen Exponenten von 0 bis 28 erhöht wird.

bits ist ein vier Elemente langes Array von 32-Bit-Ganzzahlen mit Vorzeichen.

bits [0], bits [1] und bits [2] enthalten die niedrigen, mittleren und hohen 32 Bits der ganzzahligen 96-Bit-Zahl.

bits [3] enthält den Skalierungsfaktor und das Vorzeichen und besteht aus den folgenden Teilen:

Die Bits 0 bis 15, das untere Wort, sind nicht verwendet und müssen 0 sein.

Die Bits 16 bis 23 müssen einen Exponenten zwischen 0 und 28 enthalten, der die Leistung von 10 angibt, um die ganze Zahl zu teilen.

Die Bits 24 bis 30 werden nicht verwendet und müssen 0 sein.

Bit 31 enthält das Zeichen; 0 Bedeutung positiv und 1 Bedeutung negativ.

Ein numerischer Wert kann mehrere mögliche binäre Darstellungen aufweisen. alle sind gleich gültig und numerisch gleichwertig. Beachten Sie, dass die Bitdarstellung zwischen negativer und positiver Null unterscheidet. Diese Werte werden in allen Vorgängen als gleich behandelt.

Weitere Informationen

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Decimal(Int64)

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Initialisiert eine neue Instanz von Decimal mit dem Wert einer angegebenen 64-Bit-Ganzzahl mit Vorzeichen.

C#
public Decimal (long value);

Parameter

value
Int64

Der als Decimal darzustellende Wert.

Beispiele

Im folgenden Codebeispiel werden mehrere Decimal Zahlen mithilfe der Konstruktorüberladung erstellt, die eine Decimal Struktur mit einem Int64 Wert initialisiert.

C#
// 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
*/

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Decimal(ReadOnlySpan<Int32>)

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Initialisiert eine neue instance von Decimal zu einem Dezimalwert, der in binär dargestellt und in der angegebenen Spanne enthalten ist.

C#
public Decimal (ReadOnlySpan<int> bits);

Parameter

bits
ReadOnlySpan<Int32>

Eine Spanne aus vier Int32-Werten, die eine Binärdarstellung eines Dezimalwerts enthält.

Ausnahmen

Die Länge von bits ist nicht 4, oder die Darstellung des Dezimalwerts in bits ist ungültig.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 5, 6, 7, 8, 9

Decimal(Single)

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Initialisiert eine neue Instanz von Decimal mit dem Wert der angegebenen Gleitkommazahl mit einfacher Genauigkeit.

C#
public Decimal (float value);

Parameter

value
Single

Der als Decimal darzustellende Wert.

Ausnahmen

value ist größer als Decimal.MaxValue oder kleiner als Decimal.MinValue.

- oder -

value ist NaN, PositiveInfinity oder NegativeInfinity.

Beispiele

Im folgenden Codebeispiel werden mehrere Decimal Zahlen mithilfe der Konstruktorüberladung erstellt, die eine Decimal Struktur mit einem Single Wert initialisiert.

C#
// 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
*/

Hinweise

Dieser Konstruktor rundet value mit Rundung auf 7 wichtige Ziffern ab. Dies geschieht auch dann, wenn die Zahl mehr als 7 Ziffern aufweist und die weniger signifikanten Ziffern 0 sind.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Decimal(UInt32)

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Wichtig

Diese API ist nicht CLS-kompatibel.

Initialisiert eine neue Instanz von Decimal mit dem Wert einer angegebenen 32-Bit-Ganzzahl ohne Vorzeichen.

C#
[System.CLSCompliant(false)]
public Decimal (uint value);

Parameter

value
UInt32

Der als Decimal darzustellende Wert.

Attribute

Beispiele

Im folgenden Codebeispiel werden mehrere Decimal Zahlen mithilfe der Konstruktorüberladung erstellt, die eine Decimal Struktur mit einem UInt32 Wert initialisiert.

C#
// 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
*/

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Decimal(UInt64)

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Wichtig

Diese API ist nicht CLS-kompatibel.

Initialisiert eine neue Instanz von Decimal mit dem Wert einer angegebenen 64-Bit-Ganzzahl ohne Vorzeichen.

C#
[System.CLSCompliant(false)]
public Decimal (ulong value);

Parameter

value
UInt64

Der als Decimal darzustellende Wert.

Attribute

Beispiele

Im folgenden Codebeispiel werden mehrere Decimal Zahlen mithilfe der Konstruktorüberladung erstellt, die eine Decimal Struktur mit einem UInt64 Wert initialisiert.

C#
// 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
*/

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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

Quelle:
Decimal.cs
Quelle:
Decimal.cs
Quelle:
Decimal.cs

Initialisiert einen neue Instanz von Decimal mit Parametern, die die Bestandteile der Instanz angeben.

C#
public Decimal (int lo, int mid, int hi, bool isNegative, byte scale);

Parameter

lo
Int32

Die unteren 32 Bit einer 96-Bit-Ganzzahl.

mid
Int32

Die mittleren 32 Bit einer 96-Bit-Ganzzahl.

hi
Int32

Die oberen 32 Bit einer 96-Bit-Ganzzahl.

isNegative
Boolean

true, um eine negative Zahl anzugeben; false, um eine positive Zahl anzugeben.

scale
Byte

Eine Zehnerpotenz mit einem Exponenten zwischen 0 (null) und 28.

Ausnahmen

scale ist größer als 28.

Beispiele

Im folgenden Codebeispiel werden mehrere Decimal Zahlen mithilfe der Konstruktorüberladung erstellt, die eine Decimal Struktur mit drei Int32 Wertwörtern, einem Boolean Zeichen und einem Byte Skalierungsfaktor initialisiert.

C#
// 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
*/

Im folgenden Beispiel wird die GetBits -Methode verwendet, um die Komponententeile eines Arrays abzurufen. Anschließend wird dieses Array im Aufruf des Decimal(Int32, Int32, Int32, Boolean, Byte) Konstruktors verwendet, um einen neuen Decimal Wert zu instanziieren.

C#
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

Hinweise

Die binäre Darstellung einer Decimal Zahl besteht aus einem 1-Bit-Vorzeichen, einer 96-Bit-Ganzzahl und einem Skalierungsfaktor, der verwendet wird, um die ganzzahlige Zahl zu dividieren und anzugeben, welcher Teil davon ein Dezimalbruch ist. Der Skalierungsfaktor ist implizit die Zahl 10, die auf einen Exponenten von 0 bis 28 erhöht wird.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0