Auf Englisch lesen

Freigeben über


Decimal.Explicit Operator

Definition

Definiert eine explizite Konvertierung zwischen einem Decimal-Objekt und einem anderen Typ.

Überlädt

Explicit(Single to Decimal)

Definiert eine explizite Konvertierung einer Gleitkommazahl mit einfacher Genauigkeit in eine Decimal.

Explicit(Double to Decimal)

Definiert eine explizite Konvertierung einer Gleitkommazahl mit doppelter Genauigkeit in eine Decimal.

Explicit(Decimal to UInt64)

Definiert eine explizite Konvertierung einer Decimal in eine 64-Bit Ganzzahl ohne Vorzeichen.

Diese API ist nicht CLS-kompatibel.

Explicit(Decimal to UInt32)

Definiert eine explizite Konvertierung einer Decimal in eine 32-Bit Ganzzahl ohne Vorzeichen.

Diese API ist nicht CLS-kompatibel.

Explicit(Decimal to UInt16)

Definiert eine explizite Konvertierung einer Decimal in eine 16-Bit Ganzzahl ohne Vorzeichen.

Diese API ist nicht CLS-kompatibel.

Explicit(Decimal to SByte)

Definiert eine explizite Konvertierung einer Decimal in eine 8-Bit Ganzzahl mit Vorzeichen.

Diese API ist nicht CLS-kompatibel.

Explicit(Decimal to Single)

Definiert eine explizite Konvertierung einer Decimal in eine Gleitkommazahl mit einfacher Genauigkeit.

Explicit(Decimal to Int32)

Definiert eine explizite Konvertierung einer Decimal in eine 32-Bit Ganzzahl mit Vorzeichen.

Explicit(Decimal to Int16)

Definiert eine explizite Konvertierung einer Decimal in eine 16-Bit Ganzzahl mit Vorzeichen.

Explicit(Decimal to Double)

Definiert eine explizite Konvertierung einer Decimal in eine Gleitkommazahl mit doppelter Genauigkeit.

Explicit(Decimal to Char)

Definiert eine explizite Konvertierung einer Decimal in ein Unicodezeichen.

Explicit(Decimal to Byte)

Definiert eine explizite Konvertierung einer Decimal in eine 8-Bit Ganzzahl ohne Vorzeichen.

Explicit(Decimal to Int64)

Definiert eine explizite Konvertierung einer Decimal in eine 64-Bit Ganzzahl mit Vorzeichen.

Explicit(Single to Decimal)

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

Definiert eine explizite Konvertierung einer Gleitkommazahl mit einfacher Genauigkeit in eine Decimal.

C#
public static explicit operator decimal (float value);

Parameter

value
Single

Die zu konvertierende Gleitkommazahl mit einfacher Genauigkeit.

Gibt zurück

Die konvertierte Gleitkommazahl mit einfacher Genauigkeit.

Ausnahmen

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

- oder -

value ist NaN, PositiveInfinity oder NegativeInfinity.

Beispiele

Im folgenden Beispiel werden Werte mithilfe Decimal des Konvertierungsoperators Single in Decimal Zahlen Single konvertiert. Für diese Konvertierung ist der op_Explicit-Operator in Visual Basic erforderlich.

C#
// Example of the explicit conversion from float to decimal.
using System;

class DecimalFromSingleDemo
{
    const string formatter = "{0,16:E7}{1,33}";

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

    // Convert the float argument; catch exceptions that are thrown.
    public static void DecimalFromSingle( float argument )
    {
        object decValue;

        // Convert the float argument to a decimal value.
        try
        {
            decValue = (decimal)argument;
        }
        catch( Exception ex )
        {
            decValue = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument, decValue );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the explicit conversion from float " +
            "to decimal \ngenerates the following output.\n" );
        Console.WriteLine( formatter, "float argument",
            "decimal value" );
        Console.WriteLine( formatter, "--------------",
            "-------------" );

        // Convert float values and display the results.
        DecimalFromSingle( 1.2345E-30F );
        DecimalFromSingle( 1.2345E-26F );
        DecimalFromSingle( 1.23456E-22F );
        DecimalFromSingle( 1.23456E-12F );
        DecimalFromSingle( 1.234567F );
        DecimalFromSingle( 1.234567E+12F );
        DecimalFromSingle( 1.2345678E+28F );
        DecimalFromSingle( 1.2345678E+30F );
    }
}

/*
This example of the explicit conversion from float to decimal
generates the following output.

  float argument                    decimal value
  --------------                    -------------
  1.2345000E-030                                0
  1.2345000E-026   0.0000000000000000000000000123
  1.2345600E-022    0.000000000000000000000123456
  1.2345600E-012              0.00000000000123456
  1.2345671E+000                         1.234567
  1.2345670E+012                    1234567000000
  1.2345678E+028    12345680000000000000000000000
  1.2345678E+030                OverflowException
*/

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

Explicit(Double to Decimal)

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

Definiert eine explizite Konvertierung einer Gleitkommazahl mit doppelter Genauigkeit in eine Decimal.

C#
public static explicit operator decimal (double value);

Parameter

value
Double

Die zu konvertierende Gleitkommazahl mit doppelter Genauigkeit.

Gibt zurück

Die konvertierte Gleitkommazahl mit doppelter Genauigkeit.

Ausnahmen

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

- oder -

value ist NaN, PositiveInfinity oder NegativeInfinity.

Beispiele

Im folgenden Beispiel werden Werte mithilfe Decimal des Konvertierungsoperators Double in Decimal Zahlen Double konvertiert. Für diese Konvertierung ist der op_Explicit-Operator in Visual Basic erforderlich.

C#
// Example of the explicit conversion from double to decimal.
using System;

class DecimalFromDoubleDemo
{
    const string formatter = "{0,25:E16}{1,33}";

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

    // Convert the double argument; catch exceptions that are thrown.
    public static void DecimalFromDouble( double argument )
    {
        object decValue;

        // Convert the double argument to a decimal value.
        try
        {
            decValue = (decimal)argument;
        }
        catch( Exception ex )
        {
            decValue = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument, decValue );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the explicit conversion from double " +
            "to decimal \ngenerates the following output.\n" );
        Console.WriteLine( formatter, "double argument",
            "decimal value" );
        Console.WriteLine( formatter, "---------------",
            "-------------" );

        // Convert double values and display the results.
        DecimalFromDouble( 1.234567890123E-30 );
        DecimalFromDouble( 1.2345678901234E-25 );
        DecimalFromDouble( 1.23456789012345E-20 );
        DecimalFromDouble( 1.234567890123456E-10 );
        DecimalFromDouble( 1.2345678901234567 );
        DecimalFromDouble( 1.23456789012345678E+12 );
        DecimalFromDouble( 1.234567890123456789E+28 );
        DecimalFromDouble( 1.234567890123456789E+30 );
    }
}

/*
This example of the explicit conversion from double to decimal
generates the following output.

          double argument                    decimal value
          ---------------                    -------------
  1.2345678901230000E-030                                0
  1.2345678901233999E-025   0.0000000000000000000000001235
  1.2345678901234499E-020   0.0000000000000000000123456789
  1.2345678901234560E-010       0.000000000123456789012346
  1.2345678901234567E+000                 1.23456789012346
  1.2345678901234568E+012                 1234567890123.46
  1.2345678901234568E+028    12345678901234600000000000000
  1.2345678901234569E+030                OverflowException
*/

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

Explicit(Decimal to UInt64)

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

Wichtig

Diese API ist nicht CLS-kompatibel.

Definiert eine explizite Konvertierung einer Decimal in eine 64-Bit Ganzzahl ohne Vorzeichen.

Diese API ist nicht CLS-kompatibel.

C#
[System.CLSCompliant(false)]
public static explicit operator ulong (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine 64-Bit-Ganzzahl ohne Vorzeichen, die das konvertierte Decimal darstellt.

Attribute

Ausnahmen

value ist negativ oder größer als UInt64.MaxValue.

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe UInt64 des expliziten Decimal In-Konvertierungsoperators Decimal in UInt64 Werte konvertiert.

C#
// Example of the explicit conversions from decimal to long and
// decimal to ulong.
using System;

class DecimalToU_Int64Demo
{
    const string formatter = "{0,25}{1,22}{2,22}";

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

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int64( decimal argument )
    {
        object Int64Value;
        object UInt64Value;

        // Convert the argument to a long value.
        try
        {
            Int64Value = (long)argument;
        }
        catch( Exception ex )
        {
            Int64Value = GetExceptionType( ex );
        }

        // Convert the argument to a ulong value.
        try
        {
            UInt64Value = (ulong)argument;
        }
        catch( Exception ex )
        {
            UInt64Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument,
            Int64Value, UInt64Value );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the explicit conversions from decimal " +
            "to long \nand decimal to ulong generates the following " +
            "output. It displays \nseveral converted decimal " +
            "values.\n" );
        Console.WriteLine( formatter, "decimal argument",
            "long/exception", "ulong/exception" );
        Console.WriteLine( formatter, "----------------",
            "--------------", "---------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int64( 123M );
        DecimalToU_Int64( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int64( 123.999M );
        DecimalToU_Int64( 18446744073709551615.999M );
        DecimalToU_Int64( 18446744073709551616M );
        DecimalToU_Int64( 9223372036854775807.999M );
        DecimalToU_Int64( 9223372036854775808M );
        DecimalToU_Int64( - 0.999M );
        DecimalToU_Int64( - 1M );
        DecimalToU_Int64( - 9223372036854775808.999M );
        DecimalToU_Int64( - 9223372036854775809M );
    }
}

/*
This example of the explicit conversions from decimal to long
and decimal to ulong generates the following output. It displays
several converted decimal values.

         decimal argument        long/exception       ulong/exception
         ----------------        --------------       ---------------
                      123                   123                   123
                  123.000                   123                   123
                  123.999                   123                   123
 18446744073709551615.999     OverflowException  18446744073709551615
     18446744073709551616     OverflowException     OverflowException
  9223372036854775807.999   9223372036854775807   9223372036854775807
      9223372036854775808     OverflowException   9223372036854775808
                   -0.999                     0                     0
                       -1                    -1     OverflowException
 -9223372036854775808.999  -9223372036854775808     OverflowException
     -9223372036854775809     OverflowException     OverflowException
*/

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein UInt64. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C# und Visual Basic explizit in einen UInt64 Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToUInt64(Decimal) -Methode oder Convert.ToUInt64(Decimal) aufrufen.

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

Explicit(Decimal to UInt32)

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

Wichtig

Diese API ist nicht CLS-kompatibel.

CLS-kompatible Alternative
System.Decimal.ToInt64(Decimal)

Definiert eine explizite Konvertierung einer Decimal in eine 32-Bit Ganzzahl ohne Vorzeichen.

Diese API ist nicht CLS-kompatibel.

C#
[System.CLSCompliant(false)]
public static explicit operator uint (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine 32-Bit-Ganzzahl ohne Vorzeichen, die das konvertierte Decimal darstellt.

Attribute

Ausnahmen

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

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe UInt32 des expliziten Decimal In-Konvertierungsoperators Decimal in UInt32 Werte konvertiert.

C#
// Example of the explicit conversions from decimal to int and
// decimal to uint.
using System;

class DecimalToU_Int32Demo
{
    const string formatter = "{0,17}{1,19}{2,19}";

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

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int32( decimal argument )
    {
        object Int32Value;
        object UInt32Value;

        // Convert the argument to an int value.
        try
        {
            Int32Value = (int)argument;
        }
        catch( Exception ex )
        {
            Int32Value = GetExceptionType( ex );
        }

        // Convert the argument to a uint value.
        try
        {
            UInt32Value = (uint)argument;
        }
        catch( Exception ex )
        {
            UInt32Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument,
            Int32Value, UInt32Value );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the explicit conversions from decimal " +
            "to int \nand decimal to uint generates the following " +
            "output. It displays \nseveral converted decimal " +
            "values.\n" );
        Console.WriteLine( formatter, "decimal argument",
            "int/exception", "uint/exception" );
        Console.WriteLine( formatter, "----------------",
            "-------------", "--------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int32( 123M );
        DecimalToU_Int32( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int32( 123.999M );
        DecimalToU_Int32( 4294967295.999M );
        DecimalToU_Int32( 4294967296M );
        DecimalToU_Int32( 2147483647.999M );
        DecimalToU_Int32( 2147483648M );
        DecimalToU_Int32( - 0.999M );
        DecimalToU_Int32( - 1M );
        DecimalToU_Int32( - 2147483648.999M );
        DecimalToU_Int32( - 2147483649M );
    }
}

/*
This example of the explicit conversions from decimal to int
and decimal to uint generates the following output. It displays
several converted decimal values.

 decimal argument      int/exception     uint/exception
 ----------------      -------------     --------------
              123                123                123
          123.000                123                123
          123.999                123                123
   4294967295.999  OverflowException         4294967295
       4294967296  OverflowException  OverflowException
   2147483647.999         2147483647         2147483647
       2147483648  OverflowException         2147483648
           -0.999                  0                  0
               -1                 -1  OverflowException
  -2147483648.999        -2147483648  OverflowException
      -2147483649  OverflowException  OverflowException
*/

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein UInt32. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C# und Visual Basic explizit in einen UInt32 Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToUInt32(Decimal) -Methode oder Convert.ToUInt32(Decimal) aufrufen.

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

Explicit(Decimal to UInt16)

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

Wichtig

Diese API ist nicht CLS-kompatibel.

Definiert eine explizite Konvertierung einer Decimal in eine 16-Bit Ganzzahl ohne Vorzeichen.

Diese API ist nicht CLS-kompatibel.

C#
[System.CLSCompliant(false)]
public static explicit operator ushort (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine 16-Bit-Ganzzahl ohne Vorzeichen, die das konvertierte Decimal darstellt.

Attribute

Ausnahmen

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

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe UInt16 des expliziten Decimal In-Konvertierungsoperators Decimal in UInt16 Werte konvertiert.

C#
// Example of the explicit conversions from decimal to short and
// decimal to ushort.
using System;

class DecimalToU_Int16Demo
{
    const string formatter = "{0,16}{1,19}{2,19}";

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

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int16( decimal argument )
    {
        object Int16Value;
        object UInt16Value;

        // Convert the argument to a short value.
        try
        {
            Int16Value = (short)argument;
        }
        catch( Exception ex )
        {
            Int16Value = GetExceptionType( ex );
        }

        // Convert the argument to a ushort value.
        try
        {
            UInt16Value = (ushort)argument;
        }
        catch( Exception ex )
        {
            UInt16Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument,
            Int16Value, UInt16Value );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the explicit conversions from decimal " +
            "to short \nand decimal to ushort generates the " +
            "following output. It displays \nseveral converted " +
            "decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument",
            "short/exception", "ushort/exception" );
        Console.WriteLine( formatter, "----------------",
            "---------------", "----------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int16( 123M );
        DecimalToU_Int16( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int16( 123.999M );
        DecimalToU_Int16( 65535.999M );
        DecimalToU_Int16( 65536M );
        DecimalToU_Int16( 32767.999M );
        DecimalToU_Int16( 32768M );
        DecimalToU_Int16( - 0.999M );
        DecimalToU_Int16( - 1M );
        DecimalToU_Int16( - 32768.999M );
        DecimalToU_Int16( - 32769M );
    }
}

/*
This example of the explicit conversions from decimal to short
and decimal to ushort generates the following output. It displays
several converted decimal values.

decimal argument    short/exception   ushort/exception
----------------    ---------------   ----------------
             123                123                123
         123.000                123                123
         123.999                123                123
       65535.999  OverflowException              65535
           65536  OverflowException  OverflowException
       32767.999              32767              32767
           32768  OverflowException              32768
          -0.999                  0                  0
              -1                 -1  OverflowException
      -32768.999             -32768  OverflowException
          -32769  OverflowException  OverflowException
*/

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein UInt16. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C# und Visual Basic explizit in einen UInt16 Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToUInt16(Decimal) -Methode oder Convert.ToUInt16(Decimal) aufrufen.

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

Explicit(Decimal to SByte)

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

Wichtig

Diese API ist nicht CLS-kompatibel.

CLS-kompatible Alternative
System.Decimal.ToInt16(Decimal)

Definiert eine explizite Konvertierung einer Decimal in eine 8-Bit Ganzzahl mit Vorzeichen.

Diese API ist nicht CLS-kompatibel.

C#
[System.CLSCompliant(false)]
public static explicit operator sbyte (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine 8-Bit-Ganzzahl mit Vorzeichen, die das konvertierte Decimal darstellt.

Attribute

Ausnahmen

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

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe SByte des expliziten Decimal In-Konvertierungsoperators Decimal in SByte Werte konvertiert.

C#
using System;

class Example
{
    public static void Main()
    {
        // Define an array of decimal values.
       decimal[] values = { 78m, new Decimal(78000, 0, 0, false, 3),
                            78.999m, 255.999m, 256m, 127.999m,
                            128m, -0.999m, -1m, -128.999m, -129m };
       foreach (var value in values) {
          try {
              SByte byteValue = (SByte) value;
              Console.WriteLine("{0} ({1}) --> {2} ({3})", value,
                                value.GetType().Name, byteValue,
                                byteValue.GetType().Name);
           }
           catch (OverflowException) {
              Console.WriteLine("OverflowException: Cannot convert {0}",
                                value);
           }
       }
    }
}
// The example displays the following output:
//       78 (Decimal) --> 78 (SByte)
//       78.000 (Decimal) --> 78 (SByte)
//       78.999 (Decimal) --> 78 (SByte)
//       OverflowException: Cannot convert 255.999
//       OverflowException: Cannot convert 256
//       127.999 (Decimal) --> 127 (SByte)
//       OverflowException: Cannot convert 128
//       -0.999 (Decimal) --> 0 (SByte)
//       -1 (Decimal) --> -1 (SByte)
//       -128.999 (Decimal) --> -128 (SByte)
//       OverflowException: Cannot convert -129

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein SByte. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert explizit mit C# und Visual Basic in einen Byte und einen SByte Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToSByte(Decimal) -Methode oder - Convert.ToSByte(Decimal) Methode aufrufen.

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

Explicit(Decimal to Single)

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

Definiert eine explizite Konvertierung einer Decimal in eine Gleitkommazahl mit einfacher Genauigkeit.

C#
public static explicit operator float (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine Gleitkommazahl mit einfacher Genauigkeit, die das konvertierte Decimal darstellt.

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe Single des expliziten Decimal In-Konvertierungsoperators Decimal in Single Werte konvertiert.

C#
using System;

class Example
{
    public static void Main( )
    {
        // Define an array of decimal values.
        decimal[] values = { 0.0000000000000000000000000001M,
                             0.0000000000123456789123456789M,
                             123M, new decimal(123000000, 0, 0, false, 6),
                             123456789.123456789M,
                             123456789123456789123456789M,
                             decimal.MinValue, decimal.MaxValue };
        // Convert each value to a double.
        foreach (var value in values) {
            float dblValue = (float) value;
            Console.WriteLine("{0} ({1}) --> {2} ({3})", value,
                              value.GetType().Name, dblValue,
                              dblValue.GetType().Name);
       }
    }
}
// The example displays the following output:
//    0.0000000000000000000000000001 (Decimal) --> 1E-28 (Single)
//    0.0000000000123456789123456789 (Decimal) --> 1.234568E-11 (Single)
//    123 (Decimal) --> 123 (Single)
//    123.000000 (Decimal) --> 123 (Single)
//    123456789.123456789 (Decimal) --> 1.234568E+08 (Single)
//    123456789123456789123456789 (Decimal) --> 1.234568E+26 (Single)
//    -79228162514264337593543950335 (Decimal) --> -7.922816E+28 (Single)
//    79228162514264337593543950335 (Decimal) --> 7.922816E+28 (Single)

Hinweise

Dieser Vorgang kann zu einem Genauigkeitsverlust führen, da eine Gleitkommazahl mit nur einer Genauigkeit weniger signifikante Ziffern aufweist als eine Decimal.

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein Single. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C# und Visual Basic explizit in einen Single Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToSingle(Decimal) -Methode oder Convert.ToSingle(Decimal) aufrufen.

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

Explicit(Decimal to Int32)

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

Definiert eine explizite Konvertierung einer Decimal in eine 32-Bit Ganzzahl mit Vorzeichen.

C#
public static explicit operator int (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine 32-Bit-Ganzzahl mit Vorzeichen, die das konvertierte Decimal darstellt.

Ausnahmen

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

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe Int32 des expliziten Decimal In-Konvertierungsoperators Decimal in Int32 Werte konvertiert.

C#
// Example of the explicit conversions from decimal to int and
// decimal to uint.
using System;

class DecimalToU_Int32Demo
{
    const string formatter = "{0,17}{1,19}{2,19}";

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

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int32( decimal argument )
    {
        object Int32Value;
        object UInt32Value;

        // Convert the argument to an int value.
        try
        {
            Int32Value = (int)argument;
        }
        catch( Exception ex )
        {
            Int32Value = GetExceptionType( ex );
        }

        // Convert the argument to a uint value.
        try
        {
            UInt32Value = (uint)argument;
        }
        catch( Exception ex )
        {
            UInt32Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument,
            Int32Value, UInt32Value );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the explicit conversions from decimal " +
            "to int \nand decimal to uint generates the following " +
            "output. It displays \nseveral converted decimal " +
            "values.\n" );
        Console.WriteLine( formatter, "decimal argument",
            "int/exception", "uint/exception" );
        Console.WriteLine( formatter, "----------------",
            "-------------", "--------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int32( 123M );
        DecimalToU_Int32( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int32( 123.999M );
        DecimalToU_Int32( 4294967295.999M );
        DecimalToU_Int32( 4294967296M );
        DecimalToU_Int32( 2147483647.999M );
        DecimalToU_Int32( 2147483648M );
        DecimalToU_Int32( - 0.999M );
        DecimalToU_Int32( - 1M );
        DecimalToU_Int32( - 2147483648.999M );
        DecimalToU_Int32( - 2147483649M );
    }
}

/*
This example of the explicit conversions from decimal to int
and decimal to uint generates the following output. It displays
several converted decimal values.

 decimal argument      int/exception     uint/exception
 ----------------      -------------     --------------
              123                123                123
          123.000                123                123
          123.999                123                123
   4294967295.999  OverflowException         4294967295
       4294967296  OverflowException  OverflowException
   2147483647.999         2147483647         2147483647
       2147483648  OverflowException         2147483648
           -0.999                  0                  0
               -1                 -1  OverflowException
  -2147483648.999        -2147483648  OverflowException
      -2147483649  OverflowException  OverflowException
*/

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein Int32. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C#, C++ und Visual Basic explizit in einen Int32 Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToInt32(Decimal) -Methode oder Convert.ToInt32(Decimal) aufrufen.

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

Explicit(Decimal to Int16)

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

Definiert eine explizite Konvertierung einer Decimal in eine 16-Bit Ganzzahl mit Vorzeichen.

C#
public static explicit operator short (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine 16-Bit-Ganzzahl mit Vorzeichen, die das konvertierte Decimal darstellt.

Ausnahmen

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

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe Int16 des expliziten Decimal In-Konvertierungsoperators Decimal in Int16 Werte konvertiert.

C#
// Example of the explicit conversions from decimal to short and
// decimal to ushort.
using System;

class DecimalToU_Int16Demo
{
    const string formatter = "{0,16}{1,19}{2,19}";

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

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int16( decimal argument )
    {
        object Int16Value;
        object UInt16Value;

        // Convert the argument to a short value.
        try
        {
            Int16Value = (short)argument;
        }
        catch( Exception ex )
        {
            Int16Value = GetExceptionType( ex );
        }

        // Convert the argument to a ushort value.
        try
        {
            UInt16Value = (ushort)argument;
        }
        catch( Exception ex )
        {
            UInt16Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument,
            Int16Value, UInt16Value );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the explicit conversions from decimal " +
            "to short \nand decimal to ushort generates the " +
            "following output. It displays \nseveral converted " +
            "decimal values.\n" );
        Console.WriteLine( formatter, "decimal argument",
            "short/exception", "ushort/exception" );
        Console.WriteLine( formatter, "----------------",
            "---------------", "----------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int16( 123M );
        DecimalToU_Int16( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int16( 123.999M );
        DecimalToU_Int16( 65535.999M );
        DecimalToU_Int16( 65536M );
        DecimalToU_Int16( 32767.999M );
        DecimalToU_Int16( 32768M );
        DecimalToU_Int16( - 0.999M );
        DecimalToU_Int16( - 1M );
        DecimalToU_Int16( - 32768.999M );
        DecimalToU_Int16( - 32769M );
    }
}

/*
This example of the explicit conversions from decimal to short
and decimal to ushort generates the following output. It displays
several converted decimal values.

decimal argument    short/exception   ushort/exception
----------------    ---------------   ----------------
             123                123                123
         123.000                123                123
         123.999                123                123
       65535.999  OverflowException              65535
           65536  OverflowException  OverflowException
       32767.999              32767              32767
           32768  OverflowException              32768
          -0.999                  0                  0
              -1                 -1  OverflowException
      -32768.999             -32768  OverflowException
          -32769  OverflowException  OverflowException
*/

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein Int16. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C#, Visual Basic und C++ explizit in einen Int16 Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToInt16(Decimal) -Methode oder Convert.ToInt16(Decimal) aufrufen.

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

Explicit(Decimal to Double)

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

Definiert eine explizite Konvertierung einer Decimal in eine Gleitkommazahl mit doppelter Genauigkeit.

C#
public static explicit operator double (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine Gleitkommazahl mit doppelter Genauigkeit, die das konvertierte Decimal darstellt.

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe Double des expliziten Decimal In-Konvertierungsoperators Decimal in Double Werte konvertiert.

C#
using System;

class Example
{
    public static void Main( )
    {
        // Define an array of decimal values.
        decimal[] values = { 0.0000000000000000000000000001M,
                             0.0000000000123456789123456789M,
                             123M, new decimal(123000000, 0, 0, false, 6),
                             123456789.123456789M,
                             123456789123456789123456789M,
                             decimal.MinValue, decimal.MaxValue };
        // Convert each value to a double.
        foreach (var value in values) {
            double dblValue = (double) value;
            Console.WriteLine("{0} ({1}) --> {2} ({3})", value,
                              value.GetType().Name, dblValue,
                              dblValue.GetType().Name);
       }
    }
}
// The example displays the following output:
//    0.0000000000000000000000000001 (Decimal) --> 1E-28 (Double)
//    0.0000000000123456789123456789 (Decimal) --> 1.23456789123457E-11 (Double)
//    123 (Decimal) --> 123 (Double)
//    123.000000 (Decimal) --> 123 (Double)
//    123456789.123456789 (Decimal) --> 123456789.123457 (Double)
//    123456789123456789123456789 (Decimal) --> 1.23456789123457E+26 (Double)
//    -79228162514264337593543950335 (Decimal) --> -7.92281625142643E+28 (Double)
//    79228162514264337593543950335 (Decimal) --> 7.92281625142643E+28 (Double)

Hinweise

Dieser Vorgang kann zu einem Genauigkeitsverlust führen, da eine Gleitkommazahl mit doppelter Genauigkeit weniger signifikante Ziffern als eine Decimalaufweist.

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein Double. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C#, C++ und Visual Basic explizit in einen Double Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToDouble(Decimal) -Methode oder Convert.ToDouble(Decimal) aufrufen.

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

Explicit(Decimal to Char)

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

Definiert eine explizite Konvertierung einer Decimal in ein Unicodezeichen.

C#
public static explicit operator char (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Ein Unicode-Zeichen, das das konvertierte Decimal darstellt.

Ausnahmen

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

Beispiele

Im folgenden Beispiel werden Zahlen in Char Werte (Unicode-Zeichen) konvertiert, indem der explizite Decimal In-Konvertierungsoperator Decimal verwendet Char wird.

C#
using System;

class Example
{
   public static void Main( )
   {
      // Define an array of decimal values.
      decimal[] values = { 3.33m, 55.5m, 77.7m, 123m, 123.999m, 170m,
                           188.88m, 222m, 244m, 8217m, 8250m, 65536m,
                           -1m };
      // Convert each value to a Char.
      foreach (decimal value in values) {
         try {
            char charValue = (char) value;
            Console.WriteLine("{0} --> {1} ({2:X4})", value,
                              charValue, (ushort) charValue);
         }
         catch (OverflowException) {
            Console.WriteLine("OverflowException: Cannot convert {0}",
                              value);
         }
      }
   }
}
// The example displays the following output:
//       3.33 --> ? (0003)
//       55.5 --> 7 (0037)
//       77.7 --> M (004D)
//       123 --> { (007B)
//       123.999 --> { (007B)
//       170 --> ª (00AA)
//       188.88 --> ¼ (00BC)
//       222 --> _ (00DE)
//       244 --> ô (00F4)
//       8217 --> ' (2019)
//       8250 --> > (203A)
//       OverflowException: Cannot convert 65536
//       OverflowException: Cannot convert -1

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein Char. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben oder die Konvertierung möglicherweise überhaupt nicht unterstützen. Beispielsweise unterstützen die Visual Basic- und C++-Compiler keine explizite Decimal in eine Char Konvertierung.

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

Explicit(Decimal to Byte)

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

Definiert eine explizite Konvertierung einer Decimal in eine 8-Bit Ganzzahl ohne Vorzeichen.

C#
public static explicit operator byte (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine 8-Bit-Ganzzahl ohne Vorzeichen, die das konvertierte Decimal darstellt.

Ausnahmen

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

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe Byte des expliziten Decimal In-Konvertierungsoperators Decimal in Byte Werte konvertiert.

C#
using System;

class Example
{
    public static void Main()
    {
        // Define an array of decimal values.
       decimal[] values = { 78m, new Decimal(78000, 0, 0, false, 3),
                            78.999m, 255.999m, 256m, 127.999m,
                            128m, -0.999m, -1m, -128.999m, -129m };
       foreach (var value in values) {
          try {
              Byte byteValue = (Byte) value;
              Console.WriteLine("{0} ({1}) --> {2} ({3})", value,
                                value.GetType().Name, byteValue,
                                byteValue.GetType().Name);
           }
           catch (OverflowException) {
              Console.WriteLine("OverflowException: Cannot convert {0}",
                                value);
           }
       }
    }
}
// The example displays the following output:
//       78 (Decimal) --> 78 (Byte)
//       78.000 (Decimal) --> 78 (Byte)
//       78.999 (Decimal) --> 78 (Byte)
//       255.999 (Decimal) --> 255 (Byte)
//       OverflowException: Cannot convert 256
//       127.999 (Decimal) --> 127 (Byte)
//       128 (Decimal) --> 128 (Byte)
//       -0.999 (Decimal) --> 0 (Byte)
//       OverflowException: Cannot convert -1
//       OverflowException: Cannot convert -128.999
//       OverflowException: Cannot convert -129

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein Byte. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C#, C++ und Visual Basic explizit in einen Byte Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToByte(Decimal) -Methode oder Convert.ToByte(Decimal) aufrufen.

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

Explicit(Decimal to Int64)

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

Definiert eine explizite Konvertierung einer Decimal in eine 64-Bit Ganzzahl mit Vorzeichen.

C#
public static explicit operator long (decimal value);

Parameter

value
Decimal

Der zu konvertierende Wert.

Gibt zurück

Eine 64-Bit-Ganzzahl mit Vorzeichen, die das konvertierte Decimal darstellt.

Ausnahmen

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

Beispiele

Im folgenden Beispiel werden Zahlen mithilfe Int64 des expliziten Decimal In-Konvertierungsoperators Decimal in Int64 Werte konvertiert.

C#
// Example of the explicit conversions from decimal to long and
// decimal to ulong.
using System;

class DecimalToU_Int64Demo
{
    const string formatter = "{0,25}{1,22}{2,22}";

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

    // Convert the decimal argument; catch exceptions that are thrown.
    public static void DecimalToU_Int64( decimal argument )
    {
        object Int64Value;
        object UInt64Value;

        // Convert the argument to a long value.
        try
        {
            Int64Value = (long)argument;
        }
        catch( Exception ex )
        {
            Int64Value = GetExceptionType( ex );
        }

        // Convert the argument to a ulong value.
        try
        {
            UInt64Value = (ulong)argument;
        }
        catch( Exception ex )
        {
            UInt64Value = GetExceptionType( ex );
        }

        Console.WriteLine( formatter, argument,
            Int64Value, UInt64Value );
    }

    public static void Main( )
    {
        Console.WriteLine(
            "This example of the explicit conversions from decimal " +
            "to long \nand decimal to ulong generates the following " +
            "output. It displays \nseveral converted decimal " +
            "values.\n" );
        Console.WriteLine( formatter, "decimal argument",
            "long/exception", "ulong/exception" );
        Console.WriteLine( formatter, "----------------",
            "--------------", "---------------" );

        // Convert decimal values and display the results.
        DecimalToU_Int64( 123M );
        DecimalToU_Int64( new decimal( 123000, 0, 0, false, 3 ) );
        DecimalToU_Int64( 123.999M );
        DecimalToU_Int64( 18446744073709551615.999M );
        DecimalToU_Int64( 18446744073709551616M );
        DecimalToU_Int64( 9223372036854775807.999M );
        DecimalToU_Int64( 9223372036854775808M );
        DecimalToU_Int64( - 0.999M );
        DecimalToU_Int64( - 1M );
        DecimalToU_Int64( - 9223372036854775808.999M );
        DecimalToU_Int64( - 9223372036854775809M );
    }
}

/*
This example of the explicit conversions from decimal to long
and decimal to ulong generates the following output. It displays
several converted decimal values.

         decimal argument        long/exception       ulong/exception
         ----------------        --------------       ---------------
                      123                   123                   123
                  123.000                   123                   123
                  123.999                   123                   123
 18446744073709551615.999     OverflowException  18446744073709551615
     18446744073709551616     OverflowException     OverflowException
  9223372036854775807.999   9223372036854775807   9223372036854775807
      9223372036854775808     OverflowException   9223372036854775808
                   -0.999                     0                     0
                       -1                    -1     OverflowException
 -9223372036854775808.999  -9223372036854775808     OverflowException
     -9223372036854775809     OverflowException     OverflowException
*/

Hinweise

Dieser Operator unterstützt die explizite Konvertierung von in Decimal ein Int64. Die Syntax für solche expliziten Konvertierungen ist sprachabhängig, und einzelne Sprachcompiler können unterschiedliche Implementierungen bereitstellen und unterschiedliche Ergebnisse zurückgeben. Das Beispiel veranschaulicht die unterschiedlichen Rückgabewerte, wenn Sie einen Decimal Wert mithilfe von C# und Visual Basic explizit in einen Int64 Wert konvertieren. Um eine sprachunabhängige Konvertierung durchzuführen, können Sie die Decimal.ToInt64(Decimal) -Methode oder Convert.ToInt64(Decimal) aufrufen.

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