Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Decimal.ToUInt32 Method

Converts the value of the specified Decimal to the equivalent 32-bit unsigned integer.

This method is not CLS-compliant. The CLS-compliant alternative is the ToInt64 member. For more information about CLS compliance, see What is the Common Language Specification.

[Visual Basic]
<CLSCompliant(False)>
Public Shared Function ToUInt32( _
   ByVal d As Decimal _
) As UInt32
[C#]
[CLSCompliant(false)]
public static uint ToUInt32(
 decimal d
);
[C++]
[CLSCompliant(false)]
public: static unsigned int ToUInt32(
 Decimal d
);
[JScript]
public
   CLSCompliant(false)
static function ToUInt32(
 d : Decimal
) : UInt32;

Parameters

d
A Decimal value to convert.

Return Value

A 32-bit unsigned integer equivalent to the value of d.

Exceptions

Exception Type Condition
OverflowException d is negative or greater than UInt32.MaxValue.

Remarks

The return value is the integral part of the decimal value; fractional digits are truncated.

Example

[Visual Basic, C#, C++] The following code example converts Decimal numbers to UInt32 values using the ToUInt32 method.

[Visual Basic] 
' Example of the Decimal.ToInt32 and Decimal.ToUInt32 methods.
Imports System
Imports Microsoft.VisualBasic

Module DecimalToU_Int32Demo

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

    ' 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 Decimal argument; catch exceptions that are thrown.
    Sub DecimalToU_Int32( argument As Decimal )

        Dim Int32Value    As Object
        Dim UInt32Value   As Object

        ' Convert the argument to an Integer value.
        Try
            Int32Value = Decimal.ToInt32( argument )
        Catch ex As Exception
            Int32Value = GetExceptionType( ex )
        End Try

        ' Convert the argument to a UInt32 value.
        Try
            UInt32Value = Decimal.ToUInt32( argument )
        Catch ex As Exception
            UInt32Value = GetExceptionType( ex )
        End Try

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

    Sub Main( )

        Console.WriteLine( "This example of the " & vbCrLf & _
            "  Decimal.ToInt32( Decimal ) and " & vbCrLf & _
            "  Decimal.ToUInt32( Decimal ) " & vbCrLf & "methods " & _
            "generates the following output. It " & vbCrLf & _
            "displays several converted Decimal values." & vbCrLf )
        Console.WriteLine( formatter, "Decimal argument", _
            "Integer/exception", "UInt32/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 

' This example of the
'   Decimal.ToInt32( Decimal ) and
'   Decimal.ToUInt32( Decimal )
' methods generates the following output. It
' displays several converted Decimal values.
' 
'  Decimal argument  Integer/exception   UInt32/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

[C#] 
// Example of the decimal.ToInt32 and decimal.ToUInt32 methods.
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 = decimal.ToInt32( argument );
        }
        catch( Exception ex )
        {
            Int32Value = GetExceptionType( ex );
        }

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

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

    public static void Main( )
    {
        Console.WriteLine( "This example of the \n" +
            "  decimal.ToInt32( decimal ) and \n" +
            "  decimal.ToUInt32( decimal ) \nmethods " +
            "generates the following output. It \ndisplays " +
            "several 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
  decimal.ToInt32( decimal ) and
  decimal.ToUInt32( decimal )
methods 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
*/

[C++] 
// Example of the Decimal::ToInt32 and Decimal::ToUInt32 methods.
#using <mscorlib.dll>
using namespace System;

const __wchar_t* formatter = L"{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 = __box( Decimal::ToInt32( argument ) );
    }
    catch( Exception* ex )
    {
        Int32Value = GetExceptionType( ex );
    }

    // Convert the argument to an unsigned int value.
    try
    {
        UInt32Value = __box( Decimal::ToUInt32( argument ) );
    }
    catch( Exception* ex )
    {
        UInt32Value = GetExceptionType( ex );
    }

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

void main( )
{
    Console::WriteLine( S"This example of the \n" 
        S"  Decimal::ToInt32( Decimal ) and \n" 
        S"  Decimal::ToUInt32( Decimal ) \nmethods " 
        S"generates the following output. It \ndisplays " 
        S"several converted Decimal values.\n" );
    Console::WriteLine( formatter, S"Decimal argument", 
        S"int/exception", S"unsigned int" );
    Console::WriteLine( formatter, S"----------------", 
        S"-------------", S"------------" );

    // 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
  Decimal::ToInt32( Decimal ) and
  Decimal::ToUInt32( Decimal )
methods generates the following output. It
displays several converted Decimal values.

 Decimal argument      int/exception       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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Decimal Structure | Decimal Members | System Namespace | UInt32

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.