Decimal Narrowing Conversion (Decimal to UInt32)

Converts a Decimal to a 32-bit unsigned integer.

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

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

Public Shared Narrowing Operator CType ( _
	value As Decimal _
) As UInteger


Type: System.Decimal
A Decimal to convert.

Return Value

Type: System.UInt32
A 32-bit unsigned integer that represents the converted Decimal.


value is negative or greater than UInt32.MaxValue.

This operator supports the explicit conversion of a Decimal to a UInt32. The syntax for such explicit conversions is language-dependent, and individual language compilers can provide different implementations and return different results. The example illustrates the different return values when you explicitly convert a Decimal value to a UInt32 value by using C# and Visual Basic. To perform a conversion that is independent of language, you can call the ToUInt32 or the Convert.ToUInt32(Decimal) method.

The following code example converts Decimal numbers to UInt32 values using the explicit Decimal to UInt32 conversion.

' Example of the explicit conversions from Decimal to Integer and 
' Decimal to UInteger.
Module Example

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

   ' Convert the decimal argument catch exceptions that are thrown.
   Public 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 int value.
         Int32Value = CInt(argument)
      Catch ex As Exception
         Int32Value = ex.GetType().Name
      End Try

      ' Convert the argument to a uint value.
         UInt32Value = CUInt(argument)
      Catch ex As Exception
         UInt32Value = ex.GetType().Name
      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(formatter, "Decimal Argument", _
          "Integer/Exception", "UInteger/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
' The example displays the following output:
'     Decimal Argument  Integer/Exception UInteger/Exception
'     ----------------      -------------     --------------
'                  123                123                123
'              123.000                123                123
'              123.999                124                124
'       4294967295.999  OverflowException  OverflowException
'           4294967296  OverflowException  OverflowException
'       2147483647.999  OverflowException         2147483648
'           2147483648  OverflowException         2147483648
'               -0.999                 -1  OverflowException
'                   -1                 -1  OverflowException
'      -2147483648.999  OverflowException  OverflowException
'          -2147483649  OverflowException  OverflowException


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

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

Community Additions