当前位置: 首页>>代码示例>>C#>>正文


C# Decimal.Explicit操作符代码示例

本文整理汇总了C#中System.Decimal.Explicit操作符的典型用法代码示例。如果您正苦于以下问题:C# Decimal.Explicit操作符的具体用法?C# Decimal.Explicit怎么用?C# Decimal.Explicit使用的例子?那么恭喜您, 这里精选的操作符代码示例或许可以为您提供帮助。您也可以进一步了解该操作符所在System.Decimal的用法示例。


在下文中一共展示了Decimal.Explicit操作符的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetExceptionType

// 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 );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:54,代码来源:Decimal.Explicit

输出:

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

示例2: GetExceptionType

// 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 );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:54,代码来源:Decimal.Explicit

输出:

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

示例3: GetExceptionType

// 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
*/
开发者ID:.NET开发者,项目名称:System,代码行数:92,代码来源:Decimal.Explicit

示例4: GetExceptionType

// 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
*/
开发者ID:.NET开发者,项目名称:System,代码行数:92,代码来源:Decimal.Explicit

示例5: Main

//引入命名空间
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);
       } 
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:Decimal.Explicit

输出:

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)

示例6: Main

//引入命名空间
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);
           }
       } 
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:Decimal.Explicit

输出:

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

示例7: GetExceptionType

// 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
*/
开发者ID:.NET开发者,项目名称:System,代码行数:92,代码来源:Decimal.Explicit

示例8: Main

//引入命名空间
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);
       } 
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:Decimal.Explicit

输出:

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)

示例9: Main

//引入命名空间
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);
         }
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:Decimal.Explicit

输出:

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

示例10: Main

//引入命名空间
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);
           }
       } 
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:Decimal.Explicit

输出:

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


注:本文中的System.Decimal.Explicit操作符示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。