Decimal.Explicit Operator

Definisi

Menentukan konversi eksplisit antara Decimal objek dan jenis lain.

Overload

Explicit(Single to Decimal)

Menentukan konversi eksplisit dari angka floating-point presisi tunggal ke Decimal.

Explicit(Double to Decimal)

Menentukan konversi eksplisit dari angka floating-point presisi ganda ke Decimal.

Explicit(Decimal to UInt64)

Menentukan konversi eksplisit dari bilangan Decimal bulat yang tidak ditandatangani 64-bit.

API ini bukan kompatibel CLS.

Explicit(Decimal to UInt32)

Menentukan konversi eksplisit bilangan Decimal bulat ke bilangan bulat yang tidak ditandatangani 32-bit.

API ini bukan kompatibel CLS.

Explicit(Decimal to UInt16)

Menentukan konversi eksplisit dari bilangan Decimal bulat yang tidak ditandatangani 16-bit.

API ini bukan kompatibel CLS.

Explicit(Decimal to SByte)

Menentukan konversi eksplisit bilangan Decimal bulat bertanda tangan 8-bit.

API ini bukan kompatibel CLS.

Explicit(Decimal to Single)

Menentukan konversi eksplisit dari ke Decimal angka floating-point presisi tunggal.

Explicit(Decimal to Int32)

Menentukan konversi eksplisit bilangan Decimal bulat bertanda tangan 32-bit.

Explicit(Decimal to Int16)

Menentukan konversi eksplisit dari bilangan Decimal bulat bertanda tangan 16-bit.

Explicit(Decimal to Double)

Menentukan konversi eksplisit dari angka Decimal floating-point presisi ganda ke presisi ganda.

Explicit(Decimal to Char)

Mendefinisikan konversi eksplisit dari karakter menjadi Decimal Unicode.

Explicit(Decimal to Byte)

Menentukan konversi eksplisit dari bilangan Decimal bulat yang tidak ditandatangani 8-bit.

Explicit(Decimal to Int64)

Menentukan konversi eksplisit bilangan Decimal bulat bertanda tangan 64-bit.

Explicit(Single to Decimal)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Menentukan konversi eksplisit dari angka floating-point presisi tunggal ke Decimal.

public:
 static explicit operator System::Decimal(float value);
public static explicit operator decimal (float value);
static member op_Explicit : single -> decimal
Public Shared Narrowing Operator CType (value As Single) As Decimal

Parameter

value
Single

Angka floating-point presisi tunggal untuk dikonversi.

Mengembalikan

Angka titik mengambang presisi tunggal yang dikonversi.

Pengecualian

value lebih besar dari Decimal.MaxValue atau kurang dari Decimal.MinValue.

-atau-

value adalah NaN, PositiveInfinity, atau NegativeInfinity.

Contoh

Contoh berikut mengonversi Single nilai menjadi Decimal angka dengan menggunakan Single operator ke Decimal konversi. Konversi ini memerlukan operator op_Explicit di Visual Basic.

// Example of the explicit conversion from float to Decimal.
using namespace System;
#define formatter "{0,16:E7}{1,33}"

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType( Exception^ ex )
{
   String^ exceptionType = ex->GetType()->ToString();
   return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 );
}


// Convert the float argument; catch exceptions that are thrown.
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 );
}

int 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
*/
// 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
*/
// Example of the explicit conversion from float to decimal.
let print obj1 obj2 = printfn $"{obj1,16:E7}{obj2,33}"

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

// Convert the float argument; catch exceptions that are thrown.
let decimalFromSingle (argument: float32) =
    // Convert the float argument to a decimal value.
    try
        decimal argument
        |> print argument
    with ex ->
        getExceptionType ex
        |> print argument

printfn "This example of the explicit conversion from float to decimal \ngenerates the following output.\n"
print "float argument" "decimal value"
print "--------------" "-------------"

// 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
' Example of the explicit conversion from Single to Decimal.
Module DecimalFromSingleDemo

    Const formatter As String = "{0,16:E7}{1,33}"
 
    ' 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

    ' Convert the Single argument; catch exceptions that are thrown.
    Sub DecimalFromSingle( argument As Single )

        Dim decValue    As Object
          
        ' Convert the Single argument to a Decimal value.
        Try
            decValue = Decimal.op_Explicit( argument )
        Catch ex As Exception
            decValue = GetExceptionType( ex )
        End Try

        ' Display the Decimal.
        Console.WriteLine( formatter, argument, decValue )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the explicit conversion from Single " & _
            "to Decimal " & vbCrLf & "generates the following " & _
            "output." & vbCrLf )
        Console.WriteLine( formatter, "Single argument", _
            "Decimal value" )
        Console.WriteLine( formatter, "---------------", _
            "-------------" )
          
        ' Convert Single values and display the results.
        DecimalFromSingle( 1.2345E-30 )
        DecimalFromSingle( 1.2345E-26 )
        DecimalFromSingle( 1.23456E-22 )
        DecimalFromSingle( 1.23456E-12 )
        DecimalFromSingle( 1.234567 )
        DecimalFromSingle( 1.234567E+12 )
        DecimalFromSingle( 1.2345678E+28 ) 
        DecimalFromSingle( 1.2345678E+30 )
    End Sub 
End Module

' This example of the explicit conversion from Single to Decimal
' generates the following output.
' 
'  Single 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

Lihat juga

Berlaku untuk

Explicit(Double to Decimal)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Menentukan konversi eksplisit dari angka floating-point presisi ganda ke Decimal.

public:
 static explicit operator System::Decimal(double value);
public static explicit operator decimal (double value);
static member op_Explicit : double -> decimal
Public Shared Narrowing Operator CType (value As Double) As Decimal

Parameter

value
Double

Angka floating-point presisi ganda untuk dikonversi.

Mengembalikan

Angka titik floating presisi ganda yang dikonversi.

Pengecualian

value lebih besar dari Decimal.MaxValue atau kurang dari Decimal.MinValue.

-atau-

value adalah NaN, PositiveInfinity, atau NegativeInfinity.

Contoh

Contoh berikut mengonversi Double nilai menjadi Decimal angka dengan menggunakan Double operator ke Decimal konversi. Konversi ini memerlukan operator op_Explicit di Visual Basic.

// Example of the explicit conversion from double to Decimal.
using namespace System;
#define formatter "{0,25:E16}{1,33}"

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType( Exception^ ex )
{
   String^ exceptionType = ex->GetType()->ToString();
   return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 );
}


// Convert the double argument; catch exceptions that are thrown.
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 );
}

int 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
*/
// 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
*/
// Example of the explicit conversion from double to decimal.
let print obj1 obj2 = printfn $"{obj1,25:E16}{obj2,33}"

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

// Convert the double argument; catch exceptions that are thrown.
let decimalFromDouble (argument: double) =
    // Convert the double argument to a decimal value.
    try
        decimal argument
        |> print argument
    with ex ->
        getExceptionType ex
        |> print argument


printfn "This example of the explicit conversion from double to decimal \ngenerates the following output.\n"
print "double argument" "decimal value"
print "---------------" "-------------"

// 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
' Example of the explicit conversion from Double to Decimal.
Module DecimalFromDoubleDemo

    Const formatter As String = "{0,25:E16}{1,33}"
 
    ' 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

    ' Convert the Double argument; catch exceptions that are thrown.
    Sub DecimalFromDouble( argument As Double )

        Dim decValue    As Object
          
        ' Convert the Double argument to a Decimal value.
        Try
            decValue = Decimal.op_Explicit( argument )
        Catch ex As Exception
            decValue = GetExceptionType( ex )
        End Try

        ' Display the Decimal.
        Console.WriteLine( formatter, argument, decValue )
    End Sub 
       
    Sub Main( )

        Console.WriteLine( _
            "This example of the explicit conversion from Double " & _
            "to Decimal " & vbCrLf & "generates the following " & _
            "output." & vbCrLf )
        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 )
    End Sub 
End Module

' 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

Lihat juga

Berlaku untuk

Explicit(Decimal to UInt64)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Penting

API ini bukan kompatibel CLS.

Menentukan konversi eksplisit dari bilangan Decimal bulat yang tidak ditandatangani 64-bit.

API ini bukan kompatibel CLS.

public:
 static explicit operator System::UInt64(System::Decimal value);
[System.CLSCompliant(false)]
public static explicit operator ulong (decimal value);
[<System.CLSCompliant(false)>]
static member op_Explicit : decimal -> uint64
Public Shared Narrowing Operator CType (value As Decimal) As ULong

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Bilangan bulat 64-bit yang tidak ditandatangani yang mewakili yang dikonversi Decimal.

Atribut

Pengecualian

value negatif atau lebih besar dari UInt64.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi UInt64 nilai dengan menggunakan operator eksplisit Decimal ke UInt64 konversi.

// Example of the explicit conversions from Decimal to __int64 and 
// Decimal to unsigned __int64.
using namespace System;
#define formatter "{0,25}{1,22}{2,22}"

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType( Exception^ ex )
{
   String^ exceptionType = ex->GetType()->ToString();
   return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 );
}


// Convert the Decimal argument; catch exceptions that are thrown.
void DecimalToU_Int64( Decimal argument )
{
   Object^ Int64Value;
   Object^ UInt64Value;
   
   // Convert the argument to an __int64 value.
   try
   {
      Int64Value = (__int64)argument;
   }
   catch ( Exception^ ex ) 
   {
      Int64Value = GetExceptionType( ex );
   }

   
   // Convert the argument to an unsigned __int64 value.
   try
   {
      UInt64Value = (unsigned __int64)argument;
   }
   catch ( Exception^ ex ) 
   {
      UInt64Value = GetExceptionType( ex );
   }

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

int main()
{
   Console::WriteLine( "This example of the explicit conversions from Decimal to "
   "__int64 \nand Decimal to unsigned __int64 generates the "
   "following output. \nIt displays several converted Decimal "
   "values.\n" );
   Console::WriteLine( formatter, "Decimal argument", "__int64", "unsigned __int64" );
   Console::WriteLine( formatter, "----------------", "-------", "----------------" );
   
   // Convert Decimal values and display the results.
   DecimalToU_Int64( Decimal::Parse(  "123" ) );
   DecimalToU_Int64( Decimal(123000,0,0,false,3) );
   DecimalToU_Int64( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "18446744073709551615.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "18446744073709551616" ) );
   DecimalToU_Int64( Decimal::Parse(  "9223372036854775807.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "9223372036854775808" ) );
   DecimalToU_Int64( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "-1" ) );
   DecimalToU_Int64( Decimal::Parse(  "-9223372036854775808.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "-9223372036854775809" ) );
}

/*
This example of the explicit conversions from Decimal to __int64
and Decimal to unsigned __int64 generates the following output.
It displays several converted Decimal values.

         Decimal argument               __int64      unsigned __int64
         ----------------               -------      ----------------
                      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
*/
// 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
*/
// Example of the explicit conversions from decimal to long and
// decimal to ulong.
let print obj1 obj2 obj3 = printfn $"{obj1,25}{obj2,22}{obj3,22}"

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

// Convert the decimal argument; catch exceptions that are thrown.
let decimalToU_Int64 (argument: decimal) =
    let int32Value: obj =
        // Convert the argument to a int64 value.
        try
            int32 argument
        with ex -> getExceptionType ex

    let uint32Value: obj =
        // Convert the argument to a uint64 value.
        try
            uint32 argument
        with ex -> getExceptionType ex

    print argument int32Value uint32Value

printfn "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"

print "decimal argument" "long/exception" "ulong/exception"
print "----------------" "--------------" "---------------"

// Convert decimal values and display the results.
decimalToU_Int64 123M
decimalToU_Int64 (new decimal(123000, 0, 0, false, 3uy))
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
' Example of the explicit conversions from Decimal to Long and 
' Decimal to ULong.
Module DecimalToU_Int64Demo

    Const formatter As String = "{0,25}{1,22}{2,22}"

    ' Convert the decimal argument catch exceptions that are thrown.
    Public Sub DecimalToU_Int64(argument As Decimal)
        Dim Int64Value As Object
        Dim UInt64Value As Object

        ' Convert the argument to a long value.
        Try
            Int64Value = CLng(argument)
        Catch ex As Exception
            Int64Value = ex.GetType().Name
        End Try

        ' Convert the argument to a ulong value.
        Try
            UInt64Value = CULng(argument)
        Catch ex As Exception
            UInt64Value = ex.GetType().Name
        End Try

        Console.WriteLine(formatter, argument, _ 
            Int64Value, UInt64Value)
    End Sub

    Public Sub Main( )
        Console.WriteLine( formatter, "Decimal argument", _
            "Long/Exception", "ULong/Exception" )
        Console.WriteLine( formatter, "----------------", _
            "--------------", "---------------" )

        ' Convert decimal values and display the results.
        DecimalToU_Int64(123d)
        DecimalToU_Int64(New Decimal(123000, 0, 0, False, 3))
        DecimalToU_Int64(123.999d)
        DecimalToU_Int64(18446744073709551615.999d)
        DecimalToU_Int64(18446744073709551616d)
        DecimalToU_Int64(9223372036854775807.999d)
        DecimalToU_Int64(9223372036854775808d)
        DecimalToU_Int64(-0.999d)
        DecimalToU_Int64(-1d)
        DecimalToU_Int64(-9223372036854775808.999d)
        DecimalToU_Int64(-9223372036854775809d)
    End Sub
End Module
' The example displays the following output to the console:
'             Decimal argument        Long/Exception       ULong/Exception
'             ----------------        --------------       ---------------
'                          123                   123                   123
'                      123.000                   123                   123
'                      123.999                   124                   124
'     18446744073709551615.999     OverflowException     OverflowException
'         18446744073709551616     OverflowException     OverflowException
'      9223372036854775807.999     OverflowException   9223372036854775808
'          9223372036854775808     OverflowException   9223372036854775808
'                       -0.999                    -1     OverflowException
'                           -1                    -1     OverflowException
'     -9223372036854775808.999     OverflowException     OverflowException
'         -9223372036854775809     OverflowException     OverflowException

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalUInt64. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individual dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi UInt64 nilai dengan menggunakan C# dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToUInt64(Decimal) atau Convert.ToUInt64(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to UInt32)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Penting

API ini bukan kompatibel CLS.

Alternatif kompatibel CLS
System.Decimal.ToInt64(Decimal)

Menentukan konversi eksplisit bilangan Decimal bulat ke bilangan bulat yang tidak ditandatangani 32-bit.

API ini bukan kompatibel CLS.

public:
 static explicit operator System::UInt32(System::Decimal value);
[System.CLSCompliant(false)]
public static explicit operator uint (decimal value);
[<System.CLSCompliant(false)>]
static member op_Explicit : decimal -> uint32
Public Shared Narrowing Operator CType (value As Decimal) As UInteger

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Bilangan bulat 32-bit yang tidak ditandatangani yang mewakili yang dikonversi Decimal.

Atribut

Pengecualian

value kurang dari UInt32.MinValue atau lebih besar dari UInt32.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi UInt32 nilai dengan menggunakan operator eksplisit Decimal ke UInt32 konversi.

// Example of the explicit conversions from Decimal to int and 
// Decimal to unsigned int.
using namespace System;
#define formatter "{0,17}{1,19}{2,19}"

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType( Exception^ ex )
{
   String^ exceptionType = ex->GetType()->ToString();
   return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 );
}


// Convert the Decimal argument; catch exceptions that are thrown.
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 an unsigned int value.
   try
   {
      UInt32Value = (unsigned int)argument;
   }
   catch ( Exception^ ex ) 
   {
      UInt32Value = GetExceptionType( ex );
   }

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

int main()
{
   Console::WriteLine( "This example of the explicit conversions from Decimal to "
   "int \nand Decimal to unsigned int generates the "
   "following output. \nIt displays several converted Decimal "
   "values.\n" );
   Console::WriteLine( formatter, "Decimal argument", "int", "unsigned int" );
   Console::WriteLine( formatter, "----------------", "---", "------------" );
   
   // Convert Decimal values and display the results.
   DecimalToU_Int32( Decimal::Parse(  "123" ) );
   DecimalToU_Int32( Decimal(123000,0,0,false,3) );
   DecimalToU_Int32( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "4294967295.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "4294967296" ) );
   DecimalToU_Int32( Decimal::Parse(  "2147483647.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "2147483648" ) );
   DecimalToU_Int32( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "-1" ) );
   DecimalToU_Int32( Decimal::Parse(  "-2147483648.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "-2147483649" ) );
}

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

 Decimal argument                int       unsigned int
 ----------------                ---       ------------
              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
*/
// 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
*/
// Example of the explicit conversions from decimal to int and
// decimal to uint.
let print obj1 obj2 obj3 = printfn $"{obj1,17}{obj2,19}{obj3,19}"

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

// Convert the decimal argument; catch exceptions that are thrown.
let decimalToU_Int32 (argument: decimal) =
    let int32Value: obj =
        // Convert the argument to a int32 value.
        try
            int32 argument
        with ex -> getExceptionType ex

    let uint32Value: obj =
        // Convert the argument to a uint32 value.
        try
            uint32 argument
        with ex -> getExceptionType ex

    print argument int32Value uint32Value

printfn "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"

print "decimal argument" "int/exception" "uint/exception"
print "----------------" "-------------" "--------------" 

// Convert decimal values and display the results.
decimalToU_Int32 123M
decimalToU_Int32 (new decimal(123000, 0, 0, false, 3uy))
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
' Example of the explicit conversions from Decimal to Integer and 
' Decimal to UInteger.
Module DecimalToU_Int32Demo

    Const formatter As String = "{0,17}{1,19}{2,19}"

    ' Convert the decimal argument catch exceptions that are thrown.
    Public Sub DecimalToU_Int32(argument As Decimal)
        Dim Int32Value As Object
        Dim UInt32Value As Object

        ' Convert the argument to an int value.
        Try
            Int32Value = CInt(argument)
        Catch ex As Exception 
            Int32Value = ex.GetType().Name
        End Try

        ' Convert the argument to a uint value.
        Try
            UInt32Value = CUInt(argument)
        Catch ex As Exception
            UInt32Value = ex.GetType().Name
        End Try

        Console.WriteLine(formatter, argument, _
            Int32Value, UInt32Value)
    End Sub

    Public Sub Main( )
        Console.WriteLine( formatter, "Decimal Argument", _ 
            "Integer/Exception", "UInteger/Exception" )
        Console.WriteLine( formatter, "----------------", _ 
            "-------------", "--------------" )

        ' Convert decimal values and display the results.
        DecimalToU_Int32( 123d)
        DecimalToU_Int32(New Decimal(123000, 0, 0, False, 3))
        DecimalToU_Int32(123.999d)
        DecimalToU_Int32(4294967295.999d)
        DecimalToU_Int32(4294967296d)
        DecimalToU_Int32(2147483647.999d)
        DecimalToU_Int32(2147483648d)
        DecimalToU_Int32(-0.999d)
        DecimalToU_Int32(-1d)
        DecimalToU_Int32(-2147483648.999d)
        DecimalToU_Int32(-2147483649d)
    End Sub
End Module
' The example displays the following output to the console:
'     Decimal Argument  Integer/Exception UInteger/Exception
'     ----------------      -------------     --------------
'                  123                123                123
'              123.000                123                123
'              123.999                124                124
'       4294967295.999  OverflowException  OverflowException
'           4294967296  OverflowException  OverflowException
'       2147483647.999  OverflowException         2147483648
'           2147483648  OverflowException         2147483648
'               -0.999                 -1  OverflowException
'                   -1                 -1  OverflowException
'      -2147483648.999  OverflowException  OverflowException
'          -2147483649  OverflowException  OverflowException

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalUInt32. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individual dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi UInt32 nilai dengan menggunakan C# dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToUInt32(Decimal) atau Convert.ToUInt32(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to UInt16)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Penting

API ini bukan kompatibel CLS.

Menentukan konversi eksplisit dari bilangan Decimal bulat yang tidak ditandatangani 16-bit.

API ini bukan kompatibel CLS.

public:
 static explicit operator System::UInt16(System::Decimal value);
[System.CLSCompliant(false)]
public static explicit operator ushort (decimal value);
[<System.CLSCompliant(false)>]
static member op_Explicit : decimal -> uint16
Public Shared Narrowing Operator CType (value As Decimal) As UShort

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Bilangan bulat 16-bit yang tidak ditandatangani yang mewakili yang dikonversi Decimal.

Atribut

Pengecualian

value kurang dari UInt16.MinValue atau lebih besar dari UInt16.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi UInt16 nilai dengan menggunakan operator eksplisit Decimal ke UInt16 konversi.

// Example of the explicit conversions from Decimal to short and 
// Decimal to unsigned short.
using namespace System;
#define formatter "{0,16}{1,19}{2,19}"

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType( Exception^ ex )
{
   String^ exceptionType = ex->GetType()->ToString();
   return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 );
}


// Convert the Decimal argument; catch exceptions that are thrown.
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 an unsigned short value.
   try
   {
      UInt16Value = (unsigned short)argument;
   }
   catch ( Exception^ ex ) 
   {
      UInt16Value = GetExceptionType( ex );
   }

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

int main()
{
   Console::WriteLine( "This example of the explicit conversions from Decimal to "
   "short \nand Decimal to unsigned short generates the "
   "following output. \nIt displays several converted Decimal "
   "values.\n" );
   Console::WriteLine( formatter, "Decimal argument", "short", "unsigned short" );
   Console::WriteLine( formatter, "----------------", "-----", "--------------" );
   
   // Convert Decimal values and display the results.
   DecimalToU_Int16( Decimal::Parse(  "123" ) );
   DecimalToU_Int16( Decimal(123000,0,0,false,3) );
   DecimalToU_Int16( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "65535.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "65536" ) );
   DecimalToU_Int16( Decimal::Parse(  "32767.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "32768" ) );
   DecimalToU_Int16( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "-1" ) );
   DecimalToU_Int16( Decimal::Parse(  "-32768.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "-32769" ) );
}

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

Decimal argument              short     unsigned short
----------------              -----     --------------
             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
*/
// 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
*/
// Example of the explicit conversions from decimal to short and
// decimal to ushort.
let print obj1 obj2 obj3 =
    printfn $"{obj1, 16}{obj2, 19}{obj3, 19}"

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

// Convert the decimal argument; catch exceptions that are thrown.
let decimalToU_Int16 (argument: decimal) =
    let int16Value: obj =
        // Convert the argument to a int16 value.
        try
            int16 argument
        with ex -> getExceptionType ex

    let uint16Value: obj =
        // Convert the argument to a uint16 value.
        try
            uint16 argument
        with ex -> getExceptionType ex

    print argument int16Value uint16Value

printfn "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"
print "decimal argument" "short/exception" "ushort/exception"
print "----------------" "---------------" "----------------"

// Convert decimal values and display the results.
decimalToU_Int16 123M
decimalToU_Int16 (new decimal (123000, 0, 0, false, 3uy))
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
' Example of the explicit conversions from Decimal to Short and 
' Decimal to UShort.
Module DecimalToU_Int16Demo

    Const formatter As String = "{0,16}{1,19}{2,19}"

    ' Convert the decimal argument and catch exceptions that are thrown.
    Public Sub DecimalToU_Int16(argument As Decimal)
        Dim Int16Value As Object
        Dim UInt16Value As Object

        ' Convert the argument to a Short value.
        Try
            Int16Value = CShort(argument)
        Catch ex As Exception 
            Int16Value = ex.GetType().Name
        End Try

        ' Convert the argument to a UShort value.
        Try
            UInt16Value = CUShort(argument)
        Catch ex As Exception
            UInt16Value = ex.GetType().Name
        End Try

        Console.WriteLine( formatter, argument, _
            Int16Value, UInt16Value )
    End Sub

    Public Sub Main( )
        Console.WriteLine( formatter, "Decimal argument", _ 
            "Short/Exception", "UShort/Exception" )
        Console.WriteLine( formatter, "----------------", _ 
            "---------------", "----------------" )

        ' Convert decimal values and display the results.
        DecimalToU_Int16(123d)
        DecimalToU_Int16(New Decimal( 123000, 0, 0, False, 3 ))
        DecimalToU_Int16(123.999d)
        DecimalToU_Int16(65535.999d)
        DecimalToU_Int16(65536d)
        DecimalToU_Int16(32767.999d)
        DecimalToU_Int16(32768d)
        DecimalToU_Int16(-0.999d)
        DecimalToU_Int16(-1d)
        DecimalToU_Int16(-32768.999d)
        DecimalToU_Int16(-32769d)
    End Sub
End Module
' This example displays the following output to the console:
'    Decimal argument    Short/Exception   UShort/Exception
'    ----------------    ---------------   ----------------
'                 123                123                123
'             123.000                123                123
'             123.999                124                124
'           65535.999  OverflowException  OverflowException
'               65536  OverflowException  OverflowException
'           32767.999  OverflowException              32768
'               32768  OverflowException              32768
'              -0.999                 -1  OverflowException
'                  -1                 -1  OverflowException
'          -32768.999  OverflowException  OverflowException
'              -32769  OverflowException  OverflowException

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalUInt16. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individual dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi UInt16 nilai dengan menggunakan C# dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToUInt16(Decimal) atau Convert.ToUInt16(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to SByte)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Penting

API ini bukan kompatibel CLS.

Alternatif kompatibel CLS
System.Decimal.ToInt16(Decimal)

Menentukan konversi eksplisit bilangan Decimal bulat bertanda tangan 8-bit.

API ini bukan kompatibel CLS.

public:
 static explicit operator System::SByte(System::Decimal value);
[System.CLSCompliant(false)]
public static explicit operator sbyte (decimal value);
[<System.CLSCompliant(false)>]
static member op_Explicit : decimal -> sbyte
Public Shared Narrowing Operator CType (value As Decimal) As SByte

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 8-bit yang mewakili yang dikonversi Decimal.

Atribut

Pengecualian

value kurang dari SByte.MinValue atau lebih besar dari SByte.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi SByte nilai dengan menggunakan operator eksplisit Decimal ke SByte konversi.

using namespace System;

void main()
{
   // Define an array of decimal values.
   array<Decimal>^ values = { Decimal::Parse("78"), 
                              Decimal(78000,0,0,false,3), 
                              Decimal::Parse("78.999"),
                              Decimal::Parse("255.999"),
                              Decimal::Parse("256"),
                              Decimal::Parse("127.999"),
                              Decimal::Parse("128"),
                              Decimal::Parse("-0.999"),
                              Decimal::Parse("-1"), 
                              Decimal::Parse("-128.999"),
                              Decimal::Parse("-129") };           
   for each (Decimal value in values) {
      try {
         SByte byteValue = (SByte) value;
         Console::WriteLine("{0} ({1}) --> {2} ({3})", value,
                            value.GetType()->Name, byteValue, 
                            byteValue.GetType()->Name);
      }
      catch (OverflowException^ e) {
          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
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
open System

// Define a list of decimal values.
let values = 
    [ 78m; Decimal(78000, 0, 0, false, 3uy)
      78.999m; 255.999m; 256m; 127.999m
      128m; -0.999m; -1m; -128.999m; -129m ]
for value in values do
    try
        let byteValue = int8 value
        printfn $"{value} ({value.GetType().Name}) --> {byteValue} ({byteValue.GetType().Name})"
    with :? OverflowException ->
        printfn $"OverflowException: Cannot convert {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
Option Strict On

Module Example
    Public Sub Main()
        ' Define an array of decimal values.
        Dim values() As Decimal = { 78d, New Decimal(78000, 0, 0, False, 3),
                                    78.999d, 255.999d, 256d, 127.999d,
                                    128d, -0.999d, -1d, -128.999d, 
                                    -129d }           
        For Each value In values
           Try
              Dim byteValue As SByte = CSByte(value)
              Console.WriteLine("{0} ({1}) --> {2} ({3})", value,
                                value.GetType().Name, byteValue, 
                                byteValue.GetType().Name)
           Catch e As OverflowException
              Console.WriteLine("OverflowException: Cannot convert {0}",
                                value)
           End Try
        Next   
    End Sub
End Module
' The example displays the following output:
'       78 (Decimal) --> 78 (SByte)
'       78.000 (Decimal) --> 78 (SByte)
'       78.999 (Decimal) --> 79 (SByte)
'       OverflowException: Cannot convert 255.999
'       OverflowException: Cannot convert 256
'       OverflowException: Cannot convert 127.999
'       OverflowException: Cannot convert 128
'       -0.999 (Decimal) --> -1 (SByte)
'       -1 (Decimal) --> -1 (SByte)
'       OverflowException: Cannot convert -128.999
'       OverflowException: Cannot convert -129

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalSByte. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individual dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi Byte dan SByte nilai dengan menggunakan C# dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToSByte(Decimal) metode atau Convert.ToSByte(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to Single)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Mendefinisikan konversi eksplisit dari ke Decimal angka floating-point presisi tunggal.

public:
 static explicit operator float(System::Decimal value);
public static explicit operator float (decimal value);
static member op_Explicit : decimal -> single
Public Shared Narrowing Operator CType (value As Decimal) As Single

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Angka floating-point presisi tunggal yang mewakili yang dikonversi Decimal.

Contoh

Contoh berikut mengonversi Decimal angka menjadi Single nilai dengan menggunakan operator eksplisit Decimal ke Single konversi.

using namespace System;

void main()
{
   // Define an array of Decimal values.
   array<Decimal>^ values = { Decimal::Parse("0.0000000000000000000000000001"), 
                              Decimal::Parse("0.0000000000123456789123456789"),
                              Decimal::Parse("123"), 
                              Decimal(123000000, 0, 0, false, 6),
                              Decimal::Parse("123456789.123456789"), 
                              Decimal::Parse("123456789123456789123456789"), 
                              Decimal::MinValue, Decimal::MaxValue };
   // Convert each value to a double.
   for each (Decimal 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)
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)
open System

// Define a list of decimal values.
let values = 
    [ 0.0000000000000000000000000001M
      0.0000000000123456789123456789M
      123M; Decimal(123000000, 0, 0, false, 6uy)
      123456789.123456789M
      123456789123456789123456789M
      Decimal.MinValue; Decimal.MaxValue ]

// Convert each value to a double.
for value in values do
    let dblValue = float32 value
    printfn $"{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)
Module Example
    Public Sub Main( )
        ' Define an array of Decimal values.
        Dim values() As Decimal= { 0.0000000000000000000000000001d, 
                                   0.0000000000123456789123456789d,
                                   123d, 
                                   New Decimal(123000000, 0, 0, False, 6),
                                   123456789.123456789d, 
                                   123456789123456789123456789d, 
                                   Decimal.MinValue, Decimal.MaxValue }
        For Each value In values
            Dim dblValue As Single = CSng(value)
            Console.WriteLine("{0} ({1}) --> {2} ({3})", value,
                              value.GetType().Name, dblValue, 
                              dblValue.GetType().Name)
        Next
    End Sub
End Module
' 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)

Keterangan

Operasi ini dapat mengakibatkan hilangnya presisi, karena angka floating-point presisi tunggal memiliki lebih sedikit digit signifikan daripada Decimal.

Operator ini mendukung konversi eksplisit ke DecimalSingle. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individu dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi Single nilai dengan menggunakan C# dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToSingle(Decimal) atau Convert.ToSingle(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to Int32)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Menentukan konversi eksplisit dari bilangan Decimal bulat bertanda tangan 32-bit.

public:
 static explicit operator int(System::Decimal value);
public static explicit operator int (decimal value);
static member op_Explicit : decimal -> int
Public Shared Narrowing Operator CType (value As Decimal) As Integer

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 32-bit yang mewakili yang dikonversi Decimal.

Pengecualian

value kurang dari Int32.MinValue atau lebih besar dari Int32.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi Int32 nilai dengan menggunakan operator eksplisit Decimal ke Int32 konversi.

// Example of the explicit conversions from Decimal to int and 
// Decimal to unsigned int.
using namespace System;
#define formatter "{0,17}{1,19}{2,19}"

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType( Exception^ ex )
{
   String^ exceptionType = ex->GetType()->ToString();
   return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 );
}


// Convert the Decimal argument; catch exceptions that are thrown.
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 an unsigned int value.
   try
   {
      UInt32Value = (unsigned int)argument;
   }
   catch ( Exception^ ex ) 
   {
      UInt32Value = GetExceptionType( ex );
   }

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

int main()
{
   Console::WriteLine( "This example of the explicit conversions from Decimal to "
   "int \nand Decimal to unsigned int generates the "
   "following output. \nIt displays several converted Decimal "
   "values.\n" );
   Console::WriteLine( formatter, "Decimal argument", "int", "unsigned int" );
   Console::WriteLine( formatter, "----------------", "---", "------------" );
   
   // Convert Decimal values and display the results.
   DecimalToU_Int32( Decimal::Parse(  "123" ) );
   DecimalToU_Int32( Decimal(123000,0,0,false,3) );
   DecimalToU_Int32( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "4294967295.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "4294967296" ) );
   DecimalToU_Int32( Decimal::Parse(  "2147483647.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "2147483648" ) );
   DecimalToU_Int32( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "-1" ) );
   DecimalToU_Int32( Decimal::Parse(  "-2147483648.999" ) );
   DecimalToU_Int32( Decimal::Parse(  "-2147483649" ) );
}

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

 Decimal argument                int       unsigned int
 ----------------                ---       ------------
              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
*/
// 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
*/
// Example of the explicit conversions from decimal to int and
// decimal to uint.
let print obj1 obj2 obj3 = printfn $"{obj1,17}{obj2,19}{obj3,19}"

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

// Convert the decimal argument; catch exceptions that are thrown.
let decimalToU_Int32 (argument: decimal) =
    let int32Value: obj =
        // Convert the argument to a int32 value.
        try
            int32 argument
        with ex -> getExceptionType ex

    let uint32Value: obj =
        // Convert the argument to a uint32 value.
        try
            uint32 argument
        with ex -> getExceptionType ex

    print argument int32Value uint32Value

printfn "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"

print "decimal argument" "int/exception" "uint/exception"
print "----------------" "-------------" "--------------" 

// Convert decimal values and display the results.
decimalToU_Int32 123M
decimalToU_Int32 (new decimal(123000, 0, 0, false, 3uy))
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
' Example of the explicit conversions from Decimal to Integer and 
' Decimal to UInteger.
Module DecimalToU_Int32Demo

    Const formatter As String = "{0,17}{1,19}{2,19}"

    ' Convert the decimal argument catch exceptions that are thrown.
    Public Sub DecimalToU_Int32(argument As Decimal)
        Dim Int32Value As Object
        Dim UInt32Value As Object

        ' Convert the argument to an int value.
        Try
            Int32Value = CInt(argument)
        Catch ex As Exception 
            Int32Value = ex.GetType().Name
        End Try

        ' Convert the argument to a uint value.
        Try
            UInt32Value = CUInt(argument)
        Catch ex As Exception
            UInt32Value = ex.GetType().Name
        End Try

        Console.WriteLine(formatter, argument, _
            Int32Value, UInt32Value)
    End Sub

    Public Sub Main( )
        Console.WriteLine( formatter, "Decimal Argument", _ 
            "Integer/Exception", "UInteger/Exception" )
        Console.WriteLine( formatter, "----------------", _ 
            "-------------", "--------------" )

        ' Convert decimal values and display the results.
        DecimalToU_Int32( 123d)
        DecimalToU_Int32(New Decimal(123000, 0, 0, False, 3))
        DecimalToU_Int32(123.999d)
        DecimalToU_Int32(4294967295.999d)
        DecimalToU_Int32(4294967296d)
        DecimalToU_Int32(2147483647.999d)
        DecimalToU_Int32(2147483648d)
        DecimalToU_Int32(-0.999d)
        DecimalToU_Int32(-1d)
        DecimalToU_Int32(-2147483648.999d)
        DecimalToU_Int32(-2147483649d)
    End Sub
End Module
' The example displays the following output to the console:
'     Decimal Argument  Integer/Exception UInteger/Exception
'     ----------------      -------------     --------------
'                  123                123                123
'              123.000                123                123
'              123.999                124                124
'       4294967295.999  OverflowException  OverflowException
'           4294967296  OverflowException  OverflowException
'       2147483647.999  OverflowException         2147483648
'           2147483648  OverflowException         2147483648
'               -0.999                 -1  OverflowException
'                   -1                 -1  OverflowException
'      -2147483648.999  OverflowException  OverflowException
'          -2147483649  OverflowException  OverflowException

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalInt32. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individu dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi Int32 nilai dengan menggunakan C#, C++, dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToInt32(Decimal) atau Convert.ToInt32(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to Int16)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Menentukan konversi eksplisit dari bilangan Decimal bulat bertanda tangan 16-bit.

public:
 static explicit operator short(System::Decimal value);
public static explicit operator short (decimal value);
static member op_Explicit : decimal -> int16
Public Shared Narrowing Operator CType (value As Decimal) As Short

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 16-bit yang mewakili yang dikonversi Decimal.

Pengecualian

value kurang dari Int16.MinValue atau lebih besar dari Int16.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi Int16 nilai dengan menggunakan operator eksplisit Decimal ke Int16 konversi.

// Example of the explicit conversions from Decimal to short and 
// Decimal to unsigned short.
using namespace System;
#define formatter "{0,16}{1,19}{2,19}"

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType( Exception^ ex )
{
   String^ exceptionType = ex->GetType()->ToString();
   return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 );
}


// Convert the Decimal argument; catch exceptions that are thrown.
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 an unsigned short value.
   try
   {
      UInt16Value = (unsigned short)argument;
   }
   catch ( Exception^ ex ) 
   {
      UInt16Value = GetExceptionType( ex );
   }

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

int main()
{
   Console::WriteLine( "This example of the explicit conversions from Decimal to "
   "short \nand Decimal to unsigned short generates the "
   "following output. \nIt displays several converted Decimal "
   "values.\n" );
   Console::WriteLine( formatter, "Decimal argument", "short", "unsigned short" );
   Console::WriteLine( formatter, "----------------", "-----", "--------------" );
   
   // Convert Decimal values and display the results.
   DecimalToU_Int16( Decimal::Parse(  "123" ) );
   DecimalToU_Int16( Decimal(123000,0,0,false,3) );
   DecimalToU_Int16( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "65535.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "65536" ) );
   DecimalToU_Int16( Decimal::Parse(  "32767.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "32768" ) );
   DecimalToU_Int16( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "-1" ) );
   DecimalToU_Int16( Decimal::Parse(  "-32768.999" ) );
   DecimalToU_Int16( Decimal::Parse(  "-32769" ) );
}

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

Decimal argument              short     unsigned short
----------------              -----     --------------
             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
*/
// 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
*/
// Example of the explicit conversions from decimal to short and
// decimal to ushort.
let print obj1 obj2 obj3 =
    printfn $"{obj1, 16}{obj2, 19}{obj3, 19}"

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

// Convert the decimal argument; catch exceptions that are thrown.
let decimalToU_Int16 (argument: decimal) =
    let int16Value: obj =
        // Convert the argument to a int16 value.
        try
            int16 argument
        with ex -> getExceptionType ex

    let uint16Value: obj =
        // Convert the argument to a uint16 value.
        try
            uint16 argument
        with ex -> getExceptionType ex

    print argument int16Value uint16Value

printfn "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"
print "decimal argument" "short/exception" "ushort/exception"
print "----------------" "---------------" "----------------"

// Convert decimal values and display the results.
decimalToU_Int16 123M
decimalToU_Int16 (new decimal (123000, 0, 0, false, 3uy))
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
' Example of the explicit conversions from Decimal to Short and 
' Decimal to UShort.
Module DecimalToU_Int16Demo

    Const formatter As String = "{0,16}{1,19}{2,19}"

    ' Convert the decimal argument and catch exceptions that are thrown.
    Public Sub DecimalToU_Int16(argument As Decimal)
        Dim Int16Value As Object
        Dim UInt16Value As Object

        ' Convert the argument to a Short value.
        Try
            Int16Value = CShort(argument)
        Catch ex As Exception 
            Int16Value = ex.GetType().Name
        End Try

        ' Convert the argument to a UShort value.
        Try
            UInt16Value = CUShort(argument)
        Catch ex As Exception
            UInt16Value = ex.GetType().Name
        End Try

        Console.WriteLine( formatter, argument, _
            Int16Value, UInt16Value )
    End Sub

    Public Sub Main( )
        Console.WriteLine( formatter, "Decimal argument", _ 
            "Short/Exception", "UShort/Exception" )
        Console.WriteLine( formatter, "----------------", _ 
            "---------------", "----------------" )

        ' Convert decimal values and display the results.
        DecimalToU_Int16(123d)
        DecimalToU_Int16(New Decimal( 123000, 0, 0, False, 3 ))
        DecimalToU_Int16(123.999d)
        DecimalToU_Int16(65535.999d)
        DecimalToU_Int16(65536d)
        DecimalToU_Int16(32767.999d)
        DecimalToU_Int16(32768d)
        DecimalToU_Int16(-0.999d)
        DecimalToU_Int16(-1d)
        DecimalToU_Int16(-32768.999d)
        DecimalToU_Int16(-32769d)
    End Sub
End Module
' This example displays the following output to the console:
'    Decimal argument    Short/Exception   UShort/Exception
'    ----------------    ---------------   ----------------
'                 123                123                123
'             123.000                123                123
'             123.999                124                124
'           65535.999  OverflowException  OverflowException
'               65536  OverflowException  OverflowException
'           32767.999  OverflowException              32768
'               32768  OverflowException              32768
'              -0.999                 -1  OverflowException
'                  -1                 -1  OverflowException
'          -32768.999  OverflowException  OverflowException
'              -32769  OverflowException  OverflowException

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalInt16. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individu dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi Int16 nilai dengan menggunakan C#, Visual Basic, dan C++. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToInt16(Decimal) atau Convert.ToInt16(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to Double)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Menentukan konversi eksplisit dari angka Decimal floating-point presisi ganda ke presisi ganda.

public:
 static explicit operator double(System::Decimal value);
public static explicit operator double (decimal value);
static member op_Explicit : decimal -> double
Public Shared Narrowing Operator CType (value As Decimal) As Double

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Angka floating-point presisi ganda yang mewakili yang dikonversi Decimal.

Contoh

Contoh berikut mengonversi Decimal angka menjadi Double nilai dengan menggunakan operator eksplisit Decimal ke Double konversi.

using namespace System;

void main()
{
   // Define an array of Decimal values.
   array<Decimal>^ values = { Decimal::Parse("0.0000000000000000000000000001"), 
                              Decimal::Parse("0.0000000000123456789123456789"),
                              Decimal::Parse("123"), 
                              Decimal(123000000, 0, 0, false, 6),
                              Decimal::Parse("123456789.123456789"), 
                              Decimal::Parse("123456789123456789123456789"), 
                              Decimal::MinValue, Decimal::MaxValue };
   // Convert each value to a double.
   for each (Decimal 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)
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)
open System

// Define a list of decimal values.
let values = 
    [ 0.0000000000000000000000000001M
      0.0000000000123456789123456789M
      123M; Decimal(123000000, 0, 0, false, 6uy)
      123456789.123456789M
      123456789123456789123456789M
      Decimal.MinValue; Decimal.MaxValue ]

// Convert each value to a double.
for value in values do
    let dblValue = double value
    printfn $"{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)
Module Example
    Public Sub Main( )
        ' Define an array of Decimal values.
        Dim values() As Decimal= { 0.0000000000000000000000000001d, 
                                   0.0000000000123456789123456789d,
                                   123d, 
                                   New Decimal(123000000, 0, 0, False, 6),
                                   123456789.123456789d, 
                                   123456789123456789123456789d, 
                                   Decimal.MinValue, Decimal.MaxValue }
        For Each value In values
            Dim dblValue As Double = CDbl(value)
            Console.WriteLine("{0} ({1}) --> {2} ({3})", value,
                              value.GetType().Name, dblValue, 
                              dblValue.GetType().Name)
        Next
    End Sub
End Module
' 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)

Keterangan

Operasi ini dapat mengakibatkan hilangnya presisi, karena angka floating-point presisi ganda memiliki lebih sedikit Decimaldigit signifikan daripada .

Operator ini mendukung konversi eksplisit ke DecimalDouble. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individu dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi Double nilai dengan menggunakan C#, C++, dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToDouble(Decimal) atau Convert.ToDouble(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to Char)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Mendefinisikan konversi eksplisit dari karakter menjadi Decimal Unicode.

public:
 static explicit operator char(System::Decimal value);
public static explicit operator char (decimal value);
static member op_Explicit : decimal -> char
Public Shared Narrowing Operator CType (value As Decimal) As Char

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Karakter Unicode yang mewakili yang dikonversi Decimal.

Pengecualian

value kurang dari Char.MinValue atau lebih besar dari Char.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi Char nilai (karakter Unicode) dengan menggunakan operator eksplisit Decimal ke Char konversi.

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
open System

// Define a list of decimal values.
let 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.
for value in values do
    try
        let charValue = char value
        printfn $"{value} --> {charValue} ({uint16 charValue:X4})"

    with :? OverflowException ->
        printfn $"OverflowException: Cannot convert {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
' Visual Basic does not support explicit Decimal-to-Char
' conversion using either CType or CChar.

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalChar. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individu dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda, atau mungkin tidak mendukung konversi sama sekali. Misalnya, pengkompilasi Visual Basic dan C++ tidak mendukung konversi secara eksplisit DecimalChar .

Lihat juga

Berlaku untuk

Explicit(Decimal to Byte)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Menentukan konversi eksplisit dari bilangan Decimal bulat yang tidak ditandatangani 8-bit.

public:
 static explicit operator System::Byte(System::Decimal value);
public static explicit operator byte (decimal value);
static member op_Explicit : decimal -> byte
Public Shared Narrowing Operator CType (value As Decimal) As Byte

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Bilangan bulat tidak bertanda 8-bit yang mewakili yang dikonversi Decimal.

Pengecualian

value kurang dari Byte.MinValue atau lebih besar dari Byte.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi Byte nilai dengan menggunakan operator eksplisit Decimal ke Byte konversi.

using namespace System;

void main()
{
   // Define an array of decimal values.
   array<Decimal>^ values = { Decimal::Parse("78"), 
                              Decimal(78000,0,0,false,3), 
                              Decimal::Parse("78.999"),
                              Decimal::Parse("255.999"),
                              Decimal::Parse("256"),
                              Decimal::Parse("127.999"),
                              Decimal::Parse("128"),
                              Decimal::Parse("-0.999"),
                              Decimal::Parse("-1"), 
                              Decimal::Parse("-128.999"),
                              Decimal::Parse("-129") };           
   for each (Decimal value in values) {
      try {
         Byte byteValue = (Byte) value;
         Console::WriteLine("{0} ({1}) --> {2} ({3})", value,
                            value.GetType()->Name, byteValue, 
                            byteValue.GetType()->Name);
      }
      catch (OverflowException^ e) {
          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
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
open System

// Define a list of decimal values.
let values = 
    [ 78m; Decimal(78000, 0, 0, false, 3uy)
      78.999m; 255.999m; 256m; 127.999m
      128m; -0.999m; -1m; -128.999m; -129m ]

for value in values do
    try
        let byteValue = byte value
        printfn $"{value} ({value.GetType().Name}) --> {byteValue} ({byteValue.GetType().Name})"

    with :? OverflowException ->
        printfn $"OverflowException: Cannot convert {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
Option Strict On

Module Example
    Public Sub Main()
        ' Define an array of decimal values.
        Dim values() As Decimal = { 78d, New Decimal(78000, 0, 0, False, 3),
                                    78.999d, 255.999d, 256d, 127.999d,
                                    128d, -0.999d, -1d, -128.999d, 
                                    -129d }           
        For Each value In values
           Try
              Dim byteValue As Byte = CByte(value)
              Console.WriteLine("{0} ({1}) --> {2} ({3})", value,
                                value.GetType().Name, byteValue, 
                                byteValue.GetType().Name)
           Catch e As OverflowException
              Console.WriteLine("OverflowException: Cannot convert {0}",
                                value)
           End Try
        Next   
    End Sub
End Module
' The example displays the following output:
'       78 (Decimal) --> 78 (Byte)
'       78.000 (Decimal) --> 78 (Byte)
'       78.999 (Decimal) --> 79 (Byte)
'       OverflowException: Cannot convert 255.999
'       OverflowException: Cannot convert 256
'       127.999 (Decimal) --> 128 (Byte)
'       128 (Decimal) --> 128 (Byte)
'       OverflowException: Cannot convert -0.999
'       OverflowException: Cannot convert -1
'       OverflowException: Cannot convert -128.999
'       OverflowException: Cannot convert -129

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalByte. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individu dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi Byte nilai dengan menggunakan C#, C++, dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToByte(Decimal) atau Convert.ToByte(Decimal) metode .

Lihat juga

Berlaku untuk

Explicit(Decimal to Int64)

Sumber:
Decimal.cs
Sumber:
Decimal.cs
Sumber:
Decimal.cs

Menentukan konversi eksplisit dari bilangan Decimal bulat bertanda tangan 64-bit.

public:
 static explicit operator long(System::Decimal value);
public static explicit operator long (decimal value);
static member op_Explicit : decimal -> int64
Public Shared Narrowing Operator CType (value As Decimal) As Long

Parameter

value
Decimal

Nilai yang akan dikonversi.

Mengembalikan

Bilangan bulat bertanda tangan 64-bit yang mewakili yang dikonversi Decimal.

Pengecualian

value kurang dari Int64.MinValue atau lebih besar dari Int64.MaxValue.

Contoh

Contoh berikut mengonversi Decimal angka menjadi Int64 nilai dengan menggunakan operator eksplisit Decimal ke Int64 konversi.

// Example of the explicit conversions from Decimal to __int64 and 
// Decimal to unsigned __int64.
using namespace System;
#define formatter "{0,25}{1,22}{2,22}"

// Get the exception type name; remove the namespace prefix.
String^ GetExceptionType( Exception^ ex )
{
   String^ exceptionType = ex->GetType()->ToString();
   return exceptionType->Substring( exceptionType->LastIndexOf( '.' ) + 1 );
}


// Convert the Decimal argument; catch exceptions that are thrown.
void DecimalToU_Int64( Decimal argument )
{
   Object^ Int64Value;
   Object^ UInt64Value;
   
   // Convert the argument to an __int64 value.
   try
   {
      Int64Value = (__int64)argument;
   }
   catch ( Exception^ ex ) 
   {
      Int64Value = GetExceptionType( ex );
   }

   
   // Convert the argument to an unsigned __int64 value.
   try
   {
      UInt64Value = (unsigned __int64)argument;
   }
   catch ( Exception^ ex ) 
   {
      UInt64Value = GetExceptionType( ex );
   }

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

int main()
{
   Console::WriteLine( "This example of the explicit conversions from Decimal to "
   "__int64 \nand Decimal to unsigned __int64 generates the "
   "following output. \nIt displays several converted Decimal "
   "values.\n" );
   Console::WriteLine( formatter, "Decimal argument", "__int64", "unsigned __int64" );
   Console::WriteLine( formatter, "----------------", "-------", "----------------" );
   
   // Convert Decimal values and display the results.
   DecimalToU_Int64( Decimal::Parse(  "123" ) );
   DecimalToU_Int64( Decimal(123000,0,0,false,3) );
   DecimalToU_Int64( Decimal::Parse(  "123.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "18446744073709551615.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "18446744073709551616" ) );
   DecimalToU_Int64( Decimal::Parse(  "9223372036854775807.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "9223372036854775808" ) );
   DecimalToU_Int64( Decimal::Parse(  "-0.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "-1" ) );
   DecimalToU_Int64( Decimal::Parse(  "-9223372036854775808.999" ) );
   DecimalToU_Int64( Decimal::Parse(  "-9223372036854775809" ) );
}

/*
This example of the explicit conversions from Decimal to __int64
and Decimal to unsigned __int64 generates the following output.
It displays several converted Decimal values.

         Decimal argument               __int64      unsigned __int64
         ----------------               -------      ----------------
                      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
*/
// 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
*/
// Example of the explicit conversions from decimal to long and
// decimal to ulong.
let print obj1 obj2 obj3 = printfn $"{obj1,25}{obj2,22}{obj3,22}"

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

// Convert the decimal argument; catch exceptions that are thrown.
let decimalToU_Int64 (argument: decimal) =
    let int32Value: obj =
        // Convert the argument to a int64 value.
        try
            int32 argument
        with ex -> getExceptionType ex

    let uint32Value: obj =
        // Convert the argument to a uint64 value.
        try
            uint32 argument
        with ex -> getExceptionType ex

    print argument int32Value uint32Value

printfn "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"

print "decimal argument" "long/exception" "ulong/exception"
print "----------------" "--------------" "---------------"

// Convert decimal values and display the results.
decimalToU_Int64 123M
decimalToU_Int64 (new decimal(123000, 0, 0, false, 3uy))
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
' Example of the explicit conversions from Decimal to Long and 
' Decimal to ULong.
Module DecimalToU_Int64Demo

    Const formatter As String = "{0,25}{1,22}{2,22}"

    ' Convert the decimal argument catch exceptions that are thrown.
    Public Sub DecimalToU_Int64(argument As Decimal)
        Dim Int64Value As Object
        Dim UInt64Value As Object

        ' Convert the argument to a long value.
        Try
            Int64Value = CLng(argument)
        Catch ex As Exception
            Int64Value = ex.GetType().Name
        End Try

        ' Convert the argument to a ulong value.
        Try
            UInt64Value = CULng(argument)
        Catch ex As Exception
            UInt64Value = ex.GetType().Name
        End Try

        Console.WriteLine(formatter, argument, _ 
            Int64Value, UInt64Value)
    End Sub

    Public Sub Main( )
        Console.WriteLine( formatter, "Decimal argument", _
            "Long/Exception", "ULong/Exception" )
        Console.WriteLine( formatter, "----------------", _
            "--------------", "---------------" )

        ' Convert decimal values and display the results.
        DecimalToU_Int64(123d)
        DecimalToU_Int64(New Decimal(123000, 0, 0, False, 3))
        DecimalToU_Int64(123.999d)
        DecimalToU_Int64(18446744073709551615.999d)
        DecimalToU_Int64(18446744073709551616d)
        DecimalToU_Int64(9223372036854775807.999d)
        DecimalToU_Int64(9223372036854775808d)
        DecimalToU_Int64(-0.999d)
        DecimalToU_Int64(-1d)
        DecimalToU_Int64(-9223372036854775808.999d)
        DecimalToU_Int64(-9223372036854775809d)
    End Sub
End Module
' The example displays the following output to the console:
'             Decimal argument        Long/Exception       ULong/Exception
'             ----------------        --------------       ---------------
'                          123                   123                   123
'                      123.000                   123                   123
'                      123.999                   124                   124
'     18446744073709551615.999     OverflowException     OverflowException
'         18446744073709551616     OverflowException     OverflowException
'      9223372036854775807.999     OverflowException   9223372036854775808
'          9223372036854775808     OverflowException   9223372036854775808
'                       -0.999                    -1     OverflowException
'                           -1                    -1     OverflowException
'     -9223372036854775808.999     OverflowException     OverflowException
'         -9223372036854775809     OverflowException     OverflowException

Keterangan

Operator ini mendukung konversi eksplisit ke DecimalInt64. Sintaks untuk konversi eksplisit tersebut bergantung pada bahasa, dan pengkompilasi bahasa individu dapat memberikan implementasi yang berbeda dan mengembalikan hasil yang berbeda. Contoh ini mengilustrasikan nilai pengembalian yang berbeda saat Anda secara eksplisit mengonversi Decimal nilai menjadi Int64 nilai dengan menggunakan C# dan Visual Basic. Untuk melakukan konversi yang tidak bergantung pada bahasa, Anda dapat memanggil Decimal.ToInt64(Decimal) atau Convert.ToInt64(Decimal) metode .

Lihat juga

Berlaku untuk