Decimal.ToUInt32 Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

This API is not CLS-compliant. The CLS-compliant alternative is ToInt64(Decimal).

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<CLSCompliantAttribute(False)> _
<SecuritySafeCriticalAttribute> _
Public Shared Function ToUInt32 ( _
    d As Decimal _
) As UInteger
[CLSCompliantAttribute(false)]
[SecuritySafeCriticalAttribute]
public static uint ToUInt32(
    decimal d
)

Parameters

Return Value

Type: System.UInt32
A 32-bit unsigned integer equivalent to the value of d.

Exceptions

Exception 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.

Examples

The following code example converts Decimal numbers to UInt32 values using the ToUInt32 method.

' Example of the Decimal.ToInt32 and Decimal.ToUInt32 methods.

Module Example

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

   ' Get the exception type name; remove the namespace prefix.
   Function GetExceptionType(ByVal 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(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal 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

      outputBlock.Text &= String.Format(formatter, argument, _
          Int32Value, UInt32Value) & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= String.Format("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) & vbCrLf
      outputBlock.Text &= String.Format(formatter, "Decimal argument", _
          "Integer/exception", "UInt32/exception") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "----------------", _
          "-----------------", "----------------") & vbCrLf

      ' Convert Decimal values and display the results.
      DecimalToU_Int32(outputBlock, 123D)
      DecimalToU_Int32(outputBlock, New Decimal(123000, 0, 0, False, 3))
      DecimalToU_Int32(outputBlock, 123.999D)
      DecimalToU_Int32(outputBlock, 4294967295.999D)
      DecimalToU_Int32(outputBlock, 4294967296D)
      DecimalToU_Int32(outputBlock, 2147483647.999D)
      DecimalToU_Int32(outputBlock, 2147483648D)
      DecimalToU_Int32(outputBlock, -0.999D)
      DecimalToU_Int32(outputBlock, -1D)
      DecimalToU_Int32(outputBlock, -2147483648.999D)
      DecimalToU_Int32(outputBlock, -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
// Example of the decimal.ToInt32 and decimal.ToUInt32 methods.
using System;

class Example
{
   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(System.Windows.Controls.TextBlock outputBlock, 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);
      }

      outputBlock.Text += String.Format(formatter, argument,
          Int32Value, UInt32Value) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += String.Format("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") + "\n";
      outputBlock.Text += String.Format(formatter, "decimal argument",
          "int/exception", "uint/exception") + "\n";
      outputBlock.Text += String.Format(formatter, "----------------",
          "-------------", "--------------") + "\n";

      // Convert decimal values and display the results.
      DecimalToU_Int32(outputBlock, 123M);
      DecimalToU_Int32(outputBlock, new decimal(123000, 0, 0, false, 3));
      DecimalToU_Int32(outputBlock, 123.999M);
      DecimalToU_Int32(outputBlock, 4294967295.999M);
      DecimalToU_Int32(outputBlock, 4294967296M);
      DecimalToU_Int32(outputBlock, 2147483647.999M);
      DecimalToU_Int32(outputBlock, 2147483648M);
      DecimalToU_Int32(outputBlock, -0.999M);
      DecimalToU_Int32(outputBlock, -1M);
      DecimalToU_Int32(outputBlock, -2147483648.999M);
      DecimalToU_Int32(outputBlock, -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
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.