Convert Decimal to int in CSharp

Description

The following code shows how to convert Decimal to int.

Example


  /* w ww .j a  v  a2 s.c o  m*/
  

using System;

class MainClass
{
    public static void DecimalToU_Int16( decimal argument )
    {
        object Int16Value = null;
        object UInt16Value = null;

        try
        {
            Int16Value = decimal.ToInt16( argument );
        }
        catch( Exception ex )
        {
            Int16Value = null;
        }

        try
        {
            UInt16Value = decimal.ToUInt16( argument );
        }
        catch( Exception ex )
        {
            UInt16Value = null;
        }

        Console.WriteLine( Int16Value);
        Console.WriteLine( UInt16Value );
    }

    public static void Main( )
    {
        DecimalToU_Int16( 123M );
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var