Decimal Explicit Conversion (Single to Decimal)

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

Converts a single-precision floating-point number to a Decimal.

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

Syntax

'Declaration
Public Shared Narrowing Operator CType ( _
    value As Single _
) As Decimal
public static explicit operator decimal (
    float value
)

Parameters

  • value
    Type: System.Single
    A single-precision floating-point number.

Return Value

Type: System.Decimal
A Decimal that represents the converted single-precision floating point number.

Exceptions

Exception Condition
OverflowException

value is less than MinValue or greater than MaxValue.

-or-

value is Single.NaN, Single.PositiveInfinity, or Single.NegativeInfinity.

Examples

The following code example converts Single values to Decimal numbers using the Single to Decimal conversion. This conversion requires the op_Explicit operator in Visual Basic.

' Example of the explicit conversion from Single to Decimal.

Module Example

   Const formatter As String = "{0,16:E7}{1,33}"

   ' 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 Single argument; catch exceptions that are thrown.
   Sub DecimalFromSingle(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal argument As Single)

      Dim decValue As Object

      ' Convert the Single argument to a Decimal value.
      Try
         decValue = Decimal.op_Explicit(argument)
      Catch ex As Exception
         decValue = GetExceptionType(ex)
      End Try

      ' Display the Decimal.
      outputBlock.Text &= String.Format(formatter, argument, decValue) & vbCrLf
   End Sub

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

      outputBlock.Text &= _
          "This example of the explicit conversion from Single " & _
          "to Decimal " & vbCrLf & "generates the following " & _
          "output." & vbCrLf & vbCrLf
      outputBlock.Text &= String.Format(formatter, "Single argument", _
          "Decimal value") & vbCrLf
      outputBlock.Text &= String.Format(formatter, "---------------", _
          "-------------") & vbCrLf

      ' Convert Single values and display the results.
      DecimalFromSingle(outputBlock, 1.2345E-30)
      DecimalFromSingle(outputBlock, 1.2345E-26)
      DecimalFromSingle(outputBlock, 1.23456E-22)
      DecimalFromSingle(outputBlock, 0.00000000000123456)
      DecimalFromSingle(outputBlock, 1.234567)
      DecimalFromSingle(outputBlock, 1234567000000.0)
      DecimalFromSingle(outputBlock, 1.2345678E+28)
      DecimalFromSingle(outputBlock, 1.2345678E+30)
   End Sub
End Module

' This example of the explicit conversion from Single to Decimal
' generates the following output.
' 
'  Single argument                    Decimal value
'  ---------------                    -------------
'   1.2345000E-030                                0
'   1.2345000E-026   0.0000000000000000000000000123
'   1.2345600E-022    0.000000000000000000000123456
'   1.2345600E-012              0.00000000000123456
'   1.2345671E+000                         1.234567
'   1.2345670E+012                    1234567000000
'   1.2345678E+028    12345680000000000000000000000
'   1.2345678E+030                OverflowException
// Example of the explicit conversion from float to decimal.
using System;

class Example
{
   const string formatter = "{0,16:E7}{1,33}";

   // 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 float argument; catch exceptions that are thrown.
   public static void DecimalFromSingle(System.Windows.Controls.TextBlock outputBlock, float argument)
   {
      object decValue;

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

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += 
          "This example of the explicit conversion from float " +
          "to decimal \ngenerates the following output.\n" + "\n";
      outputBlock.Text += String.Format(formatter, "float argument",
          "decimal value") + "\n";
      outputBlock.Text += String.Format(formatter, "--------------",
          "-------------") + "\n";

      // Convert float values and display the results.
      DecimalFromSingle(outputBlock, 1.2345E-30F);
      DecimalFromSingle(outputBlock, 1.2345E-26F);
      DecimalFromSingle(outputBlock, 1.23456E-22F);
      DecimalFromSingle(outputBlock, 1.23456E-12F);
      DecimalFromSingle(outputBlock, 1.234567F);
      DecimalFromSingle(outputBlock, 1.234567E+12F);
      DecimalFromSingle(outputBlock, 1.2345678E+28F);
      DecimalFromSingle(outputBlock, 1.2345678E+30F);
   }
}

/*
This example of the explicit conversion from float to decimal
generates the following output.

  float argument                    decimal value
  --------------                    -------------
  1.2345000E-030                                0
  1.2345000E-026   0.0000000000000000000000000123
  1.2345600E-022    0.000000000000000000000123456
  1.2345600E-012              0.00000000000123456
  1.2345671E+000                         1.234567
  1.2345670E+012                    1234567000000
  1.2345678E+028    12345680000000000000000000000
  1.2345678E+030                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.