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


C# Decimal构造函数代码示例

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


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

示例1: GetExceptionType

// Example of the decimal( double ) constructor.
using System;

class DecimalCtorDoDemo
{
    // Get the exception type name; remove the namespace prefix.
    public static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' )+1 );
    }

    // Create a decimal object and display its value.
    public static void CreateDecimal( double value, string valToStr )
    {
        // Format and display the constructor.
        Console.Write( "{0,-34}", 
            String.Format( "decimal( {0} )", valToStr ) );

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

            // Display the value if it was created successfully.
            Console.WriteLine( "{0,31}", decimalNum );
        }
        catch( Exception ex )
        {
            // Display the exception type if an exception was thrown.
            Console.WriteLine( "{0,31}", GetExceptionType( ex ) );
        }
    }
    
    public static void Main( )
    {
        Console.WriteLine( "This example of the decimal( double ) " +
            "constructor \ngenerates the following output.\n" );
        Console.WriteLine( "{0,-34}{1,31}", "Constructor", 
            "Value or Exception" );
        Console.WriteLine( "{0,-34}{1,31}", "-----------", 
            "------------------" );

        // Construct decimal objects from double values.
        CreateDecimal( 1.23456789E+5, "1.23456789E+5" );
        CreateDecimal( 1.234567890123E+15, "1.234567890123E+15" );
        CreateDecimal( 1.2345678901234567E+25, 
            "1.2345678901234567E+25" );
        CreateDecimal( 1.2345678901234567E+35, 
            "1.2345678901234567E+35" );
        CreateDecimal( 1.23456789E-5, "1.23456789E-5" );
        CreateDecimal( 1.234567890123E-15, "1.234567890123E-15" );
        CreateDecimal( 1.2345678901234567E-25, 
            "1.2345678901234567E-25" );
        CreateDecimal( 1.2345678901234567E-35, 
            "1.2345678901234567E-35" );
        CreateDecimal( 1.0 / 7.0, "1.0 / 7.0" );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:60,代码来源:Decimal

输出:

Constructor                                    Value or Exception
-----------                                    ------------------
decimal( 1.23456789E+5 )                               123456.789
decimal( 1.234567890123E+15 )                    1234567890123000
decimal( 1.2345678901234567E+25 )      12345678901234600000000000
decimal( 1.2345678901234567E+35 )               OverflowException
decimal( 1.23456789E-5 )                          0.0000123456789
decimal( 1.234567890123E-15 )       0.000000000000001234567890123
decimal( 1.2345678901234567E-25 )  0.0000000000000000000000001235
decimal( 1.2345678901234567E-35 )                               0
decimal( 1.0 / 7.0 )                            0.142857142857143

示例2: CreateDecimal

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

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

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

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

        // Construct decimal objects from int values.
        CreateDecimal( int.MinValue, "int.MinValue" );
        CreateDecimal( int.MaxValue, "int.MaxValue" );
        CreateDecimal( 0, "0" );
        CreateDecimal( 999999999, "999999999" );
        CreateDecimal( 0x40000000, "0x40000000" );
        CreateDecimal( unchecked( (int)0xC0000000 ), 
            "(int)0xC0000000" );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:34,代码来源:Decimal

输出:

Constructor                              Value
-----------                              -----
decimal( int.MinValue )            -2147483648
decimal( int.MaxValue )             2147483647
decimal( 0 )                                 0
decimal( 999999999 )                 999999999
decimal( 0x40000000 )               1073741824
decimal( (int)0xC0000000 )         -1073741824

示例3: GetExceptionType

// Example of the decimal( int[ ] ) constructor.
using System;

class DecimalCtorIArrDemo
{
    // Get the exception type name; remove the namespace prefix.
    public static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    // Create a decimal object and display its value.
    public static void CreateDecimal( int[ ] bits )
    {
        // Format the constructor for display.
        string ctor = String.Format( 
            "decimal( {{ 0x{0:X}", bits[ 0 ] );
        string valOrExc;
        
        for( int index = 1; index < bits.Length; index++ )
            ctor += String.Format( ", 0x{0:X}", bits[ index ] );
        ctor += " } )";

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

            // Format the decimal value for display.
            valOrExc = decimalNum.ToString( );
        }
        catch( Exception ex )
        {
            // Save the exception type if an exception was thrown.
            valOrExc = GetExceptionType( ex );
        }

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

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

        // Otherwise, display the data on two lines.
        else
        {
            Console.WriteLine( "{0}", ctor );
            Console.WriteLine( "{0,76}", valOrExc );
        }
    }
    
    public static void Main( )
    {
        Console.WriteLine( 
            "This example of the decimal( int[ ] ) constructor " +
            "\ngenerates the following output.\n" );
        Console.WriteLine( "{0,-38}{1,38}", "Constructor", 
            "Value or Exception" );
        Console.WriteLine( "{0,-38}{1,38}", "-----------", 
            "------------------" );

        // Construct decimal objects from integer arrays.
        CreateDecimal( new int[ ] { 0, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 1000000000, 0, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 1000000000, 0, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 1000000000, 0 } );
        CreateDecimal( new int[ ] { 0, 0, 0, 1000000000 } );
        CreateDecimal( new int[ ] { -1, -1, -1, 0 } );
        CreateDecimal( new int[ ] 
            { -1, -1, -1, unchecked( (int)0x80000000 ) } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x100000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1C0000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1D0000 } );
        CreateDecimal( new int[ ] { -1, 0, 0, 0x1C0001 } );
        CreateDecimal( new int[ ] 
            { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:84,代码来源:Decimal

输出:

Constructor                                               Value or Exception
-----------                                               ------------------
decimal( { 0x0, 0x0, 0x0, 0x0 } )                                          0
decimal( { 0x0, 0x0, 0x0 } )                               ArgumentException
decimal( { 0x0, 0x0, 0x0, 0x0, 0x0 } )                     ArgumentException
decimal( { 0x3B9ACA00, 0x0, 0x0, 0x0 } )                          1000000000
decimal( { 0x0, 0x3B9ACA00, 0x0, 0x0 } )                 4294967296000000000
decimal( { 0x0, 0x0, 0x3B9ACA00, 0x0 } )       18446744073709551616000000000
decimal( { 0x0, 0x0, 0x0, 0x3B9ACA00 } )                   ArgumentException
decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0 } )
                                               79228162514264337593543950335
decimal( { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x80000000 } )
                                              -79228162514264337593543950335
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x100000 } )             0.0000004294967295
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0000 } ) 0.0000000000000000004294967295
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1D0000 } )              ArgumentException
decimal( { 0xFFFFFFFF, 0x0, 0x0, 0x1C0001 } )              ArgumentException
decimal( { 0xF0000, 0xF0000, 0xF0000, 0xF0000 } )
                                                 18133887298.441562272235520

示例4: CreateDecimal

// Example of the decimal( long ) constructor.
using System;

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

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

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

        // Construct decimal objects from long values.
        CreateDecimal( long.MinValue, "long.MinValue" );
        CreateDecimal( long.MaxValue, "long.MaxValue" );
        CreateDecimal( 0L, "0L" );
        CreateDecimal( 999999999999999999, "999999999999999999" );
        CreateDecimal( 0x2000000000000000, "0x2000000000000000" );
        CreateDecimal( unchecked( (long)0xE000000000000000 ), 
            "(long)0xE000000000000000" );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:34,代码来源:Decimal

输出:

Constructor                                         Value
-----------                                         -----
decimal( long.MinValue )             -9223372036854775808
decimal( long.MaxValue )              9223372036854775807
decimal( 0 )                                            0
decimal( 999999999999999999 )          999999999999999999
decimal( 0x2000000000000000 )         2305843009213693952
decimal( (long)0xE000000000000000 )  -2305843009213693952

示例5: GetExceptionType

// Example of the decimal( float ) constructor.
using System;

class DecimalCtorSDemo
{
    // Get the exception type name; remove the namespace prefix.
    public static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    // Create a decimal object and display its value.
    public static void CreateDecimal( float value, string valToStr )
    {
        // Format and display the constructor.
        Console.Write( "{0,-27}", 
            String.Format( "decimal( {0} )", valToStr ) );

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

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

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

        // Construct decimal objects from float values.
        CreateDecimal( 1.2345E+5F, "1.2345E+5F" );
        CreateDecimal( 1.234567E+15F, "1.234567E+15F" );
        CreateDecimal( 1.23456789E+25F, "1.23456789E+25F" );
        CreateDecimal( 1.23456789E+35F, "1.23456789E+35F" );
        CreateDecimal( 1.2345E-5F, "1.2345E-5F" );
        CreateDecimal( 1.234567E-15F, "1.234567E-15F" );
        CreateDecimal( 1.23456789E-25F, "1.23456789E-25F" );
        CreateDecimal( 1.23456789E-35F, "1.23456789E-35F" );
        CreateDecimal( 1.0F / 7.0F, "1.0F / 7.0F" );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:57,代码来源:Decimal

输出:

Constructor                             Value or Exception
-----------                             ------------------
decimal( 1.2345E+5F )                               123450
decimal( 1.234567E+15F )                  1234567000000000
decimal( 1.23456789E+25F )      12345680000000000000000000
decimal( 1.23456789E+35F )               OverflowException
decimal( 1.2345E-5F )                          0.000012345
decimal( 1.234567E-15F )           0.000000000000001234567
decimal( 1.23456789E-25F )  0.0000000000000000000000001235
decimal( 1.23456789E-35F )                               0
decimal( 1.0F / 7.0F )                           0.1428571

示例6: CreateDecimal

// Example of the decimal( uint ) constructor.
using System;

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

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

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

        // Construct decimal objects from uint values.
        CreateDecimal( uint.MinValue, "uint.MinValue" );
        CreateDecimal( uint.MaxValue, "uint.MaxValue" );
        CreateDecimal( (uint)int.MaxValue, "(uint)int.MaxValue" );
        CreateDecimal( 999999999U, "999999999U" );
        CreateDecimal( 0x40000000U, "0x40000000U" );
        CreateDecimal( 0xC0000000, "0xC0000000" );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:33,代码来源:Decimal

输出:

Constructor                                 Value
-----------                                 -----
decimal( uint.MinValue )                        0
decimal( uint.MaxValue )               4294967295
decimal( (uint)int.MaxValue )          2147483647
decimal( 999999999U )                   999999999
decimal( 0x40000000U )                 1073741824
decimal( 0xC0000000 )                  3221225472

示例7: CreateDecimal

// Example of the decimal( ulong ) constructor.
using System;

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

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

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

        // Construct decimal objects from ulong values.
        CreateDecimal( ulong.MinValue, "ulong.MinValue" );
        CreateDecimal( ulong.MaxValue, "ulong.MaxValue" );
        CreateDecimal( long.MaxValue, "long.MaxValue" );
        CreateDecimal( 999999999999999999, "999999999999999999" );
        CreateDecimal( 0x2000000000000000, "0x2000000000000000" );
        CreateDecimal( 0xE000000000000000, "0xE000000000000000" );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:33,代码来源:Decimal

输出:

Constructor                                         Value
-----------                                         -----
decimal( ulong.MinValue )                               0
decimal( ulong.MaxValue )            18446744073709551615
decimal( long.MaxValue )              9223372036854775807
decimal( 999999999999999999 )          999999999999999999
decimal( 0x2000000000000000 )         2305843009213693952
decimal( 0xE000000000000000 )        16140901064495857664

示例8: GetExceptionType

// Example of the decimal( int, int, int, bool, byte ) constructor.
using System;

class DecimalCtorIIIBByDemo
{
    // Get the exception type name; remove the namespace prefix.
    public static string GetExceptionType( Exception ex )
    {
        string exceptionType = ex.GetType( ).ToString( );
        return exceptionType.Substring( 
            exceptionType.LastIndexOf( '.' ) + 1 );
    }

    // Create a decimal object and display its value.
    public static void CreateDecimal( int low, int mid, int high, 
        bool isNeg, byte scale )
    {
        // Format the constructor for display.
        string ctor = String.Format( 
            "decimal( {0}, {1}, {2}, {3}, {4} )", 
            low, mid, high, isNeg, scale );
        string valOrExc;

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

            // Format and save the decimal value.
            valOrExc = decimalNum.ToString( );
        }
        catch( Exception ex )
        {
            // Save the exception type if an exception was thrown.
            valOrExc = GetExceptionType( ex );
        }

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

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

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

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

        // Construct decimal objects from the component fields.
        CreateDecimal( 0, 0, 0, false, 0 );
        CreateDecimal( 0, 0, 0, false, 27 );
        CreateDecimal( 0, 0, 0, true, 0 );
        CreateDecimal( 1000000000, 0, 0, false, 0 );
        CreateDecimal( 0, 1000000000, 0, false, 0 );
        CreateDecimal( 0, 0, 1000000000, false, 0 );
        CreateDecimal( 1000000000, 1000000000, 1000000000, false, 0 );
        CreateDecimal( -1, -1, -1, false, 0 );
        CreateDecimal( -1, -1, -1, true, 0 );
        CreateDecimal( -1, -1, -1, false, 15 );
        CreateDecimal( -1, -1, -1, false, 28 );
        CreateDecimal( -1, -1, -1, false, 29 );
        CreateDecimal( int.MaxValue, 0, 0, false, 18 );
        CreateDecimal( int.MaxValue, 0, 0, false, 28 );
        CreateDecimal( int.MaxValue, 0, 0, true, 28 );
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:83,代码来源:Decimal

输出:

Constructor                                               Value or Exception
-----------                                               ------------------
decimal( 0, 0, 0, False, 0 )                                               0
decimal( 0, 0, 0, False, 27 )                                              0
decimal( 0, 0, 0, True, 0 )                                                0
decimal( 1000000000, 0, 0, False, 0 )                             1000000000
decimal( 0, 1000000000, 0, False, 0 )                    4294967296000000000
decimal( 0, 0, 1000000000, False, 0 )          18446744073709551616000000000
decimal( 1000000000, 1000000000, 1000000000, False, 0 )
                                               18446744078004518913000000000
decimal( -1, -1, -1, False, 0 )                79228162514264337593543950335
decimal( -1, -1, -1, True, 0 )                -79228162514264337593543950335
decimal( -1, -1, -1, False, 15 )              79228162514264.337593543950335
decimal( -1, -1, -1, False, 28 )              7.9228162514264337593543950335
decimal( -1, -1, -1, False, 29 )                 ArgumentOutOfRangeException
decimal( 2147483647, 0, 0, False, 18 )                  0.000000002147483647
decimal( 2147483647, 0, 0, False, 28 )        0.0000000000000000002147483647
decimal( 2147483647, 0, 0, True, 28 )        -0.0000000000000000002147483647

示例9: Main

//引入命名空间
using System;

public class Example
{
   public static void Main()
   {
      Decimal[] values = { 1234.96m, -1234.96m };
      foreach (var value in values) {
         int[] parts = Decimal.GetBits(value);
         bool sign = (parts[3] & 0x80000000) != 0;
   
         byte scale = (byte) ((parts[3] >> 16) & 0x7F); 
         Decimal newValue = new Decimal(parts[0], parts[1], parts[2], sign, scale);
         Console.WriteLine("{0} --> {1}", value, newValue);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:Decimal

输出:

1234.96 --> 1234.96
-1234.96 --> -1234.96


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