Send feedback on this topic.
Teradata.Client.Provider
Conversion To and From Base Class Library System Types
.NET Data Provider for Teradata > Developer's Guide > Data Types > Numeric Data Types > Conversion To and From Base Class Library System Types

The Numeric Provider Specific Types support implicit and explicit conversions to and from several .Net data types. When a conversion is done implicitly a cast does not have to be specified and the conversion will always succeed. For example, going from an Int16 to a TdDecimal is considered an implicit cast. An explicit conversion requires a cast to be specified and it is possible that the conversion may fail. Converting a TdNumber to an Int32 is an explicit conversion.

In addition, System.IConvertible has also been implemented for each Numeric Provider Specific Type. This will enable the conversion methods from the System.Convert class to be called to convert Provider Specific Type to another System data type.

TdDecimal and TdNumber have a higher precision when compared to .Net data types. Therefore, when a conversion is performed to a Sytem type the conversion may cause an overflow, precision may be lost, or truncation may occur. In cases where the Provider Specific Type cannot be converted because of an overflow, an OverflowException will be thrown.

The following charts indicates the type of conversion that needs to be performed when converting either from or to a .Net data type:

Provider Specific Type Data Type To Provider Specific Type From Provider Specific Type Method to Convert From Provider Specific Type Comments
TdDecimal Byte Implicit Explicit ToByte(TdDecimal)  
Char Implicit Explicit ToChar(TdDecimal)  
Double Explicit Explicit ToDouble(TdDecimal) The conversion from a Double to TdDecimal may cause an overflow.
Int16 Implicit Explicit ToInt16(TdDecimal)  
Int32 Implicit Explicit ToInt32(TdDecimal)  
Int64 Implicit Explicit ToInt64(TdDecimal)  
Decimal Implicit Explicit ToDecimal(TdDecimal)  
SByte Implicit Explicit ToSByte(TdDecimal)  
Single Explicit Explicit ToSingle(TdDecimal) The conversion from a Single to a TdDecimal may cause an overflow.
UInt16 Implicit Explicit ToUInt16(TdDecimal)  
UInt32 Implicit Explicit ToUInt32(TdDecimal)  
UInt64 Implicit Explicit ToUInt64(TdDecimal)  
TdNumber Byte Implicit Explicit ToByte(TdNumber)  
Char Implicit Explicit ToChar(TdNumber)  
Double Explicit Explicit ToDouble(TdNumber) A TdNumber must be explicitly casted to a Double because a TdNumber that represents a floating point decimal can have a scale of up to 38 digits.
Int16 Implicit Explicit ToInt16(TdNumber)  
Int32 Implicit Explicit ToInt32(TdNumber)  
Int64 Implicit Explicit ToInt64(TdNumber)  
Decimal Implicit Explicit ToDecimal(TdNumber) A System.Decimal can have a precision up to 28 digits. A TdNumber can have a precision up to 38 and can also represent a floating point decimal. Therefore, an explicit cast must be specified when converting from a TdNumber to a Decimal.
SByte Implicit Explicit ToSByte(TdNumber)  
Single Implicit Explicit ToSingle(TdNumber)  
UInt16 Implicit Explicit ToUInt16(TdNumber)  
UInt32 Implicit Explicit ToUInt32(TdNumber)  
UInt64 Implicit Explicit ToUInt64(TdNumber)  
TdDecimal Implicit Explicit

This is a Provider Specific to Provider Specific type cast. A Convert method does not exist for this casting.

A TdNumber must be explicitly casted to a TdDecimal because a TdNumber may represent a number that is less than TdDecimal.MinValue or a number greater than TdDecimal.MaxValue.

The following example shows when numbers represented as a TdDecimal will be truncated after being converted to System.Decimal:

TdDecimal TdDecimal
(Precision, Scale)
System.Decimal Comments
865745749683472365.463784965454 (30, 12) 865745749683472365.4637849654 Last two digits in fractional part have been truncated
5768390475867213464566534434235.682312 (37, 6) 5768390475867213464566534434235 Entire fractional part has been truncated.
9768343565867213464782534434235.634 (34, 3)   This number cannot be converted to a System.Decimal. The integer portion is greater than System.Decimal.MaxValue. An OverflowException will be thrown.

Parsing Numeric Strings

In order to convert a numeric string to a Numeric Provider Specific Type the Parse method must be called. An exception will be thrown if the any of the following occurs:

Currently, the parse method does not support all of the cultural formatting properties. Supported culture specific formatting properties are as follows:

Please refer to the Culture Specific Formatting section for more information.

String Conversion

To convert a Numeric Provider Specific Type to a numeric String, the method TdDecimal.ToString is called.  This method can convert the number into a numeric string that is in a culture specific format. Refer to Culture Specific Formatting for more information.

Example

The following is an example of converting a TdDecimal to/from different system types.

C#
Copy Code
public void Conversion() 
{ 
    String numericValue = "12897.34209845";
   
    // Going to convert numeric string to a TdDecimal
    TdDecimal stringToTdDecimal = TdDecimal.Parse(numericString);
   
    // Going to implicitly convert an Int32 to a TdDecimal
    Int32 valueInt32 = 98087473;
    TdDecimal int32ToTdDecimal = valueInt32;
   
    // A Double needs to be converted explicitly
    Double valueDouble = 1.342332E20;
    TdDecimal doubleToTdDecimal = (TdDecimal)valueDouble;
   
    // There are several ways to convert a TdDecimal to another type
    //    Explicitly
    //    Conversion Method
    //    Convert method
    // Number = 5556558260396752.3392642858199218667
    // When converting to Int64, the fractional part of number will be truncated.
    TdDecimal value = new TdDecimal(0x393AF9EB, 0x12137D63, 0x40AF3739, 0x000AB398, 35, 19);
    Int64 int64Temp1 = (Int64)value;
    Int64 int64Temp2 = TdDecimal.ToInt64(value);
    Int64 int64Temp3 = System.Convert.ToInt64((Object)value);
   
    // Going to convert a TdDecimal to a String
    //     tempTdDecimal = 3248765.213857463 (precision = 16, scale = 9)
    TdDecimal tempTdDecimal =
         new TdDecimal(0x1887D6B7, 0x000B8ABC, 0, 0, 16, 9);
    String resultStr1 = tempTdDecimal.ToString();
    String resultStr2 = tempTdDecimal.ToString("f20");
    String resultStr3 = tempTdDecimal.ToString("e5");
    String resultStr4 = tempTdDecimal.ToString("E10");
    String resultStr5 = tempTdDecimal.ToString("C2");
   
    // The results for the conversions will be
    //    resultStr1     "3248765.213857463"
    //    resultStr2     "3248765.21385746300000000000"
    //    resultStr3     "3.24877e+006"
    //    resultStr4     "3.2487652139E+006"
    //    resultStr5     "$3,248,765.21"
   
    
}