Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 423

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/session/session.php on line 426

Warning: Cannot modify header information - headers already sent by (output started at /home/content/f/e/r/ferasferas1/html/scriptslibrary/libraries/joomla/database/database.php:2) in /home/content/f/e/r/ferasferas1/html/scriptslibrary/templates/ja_purity/ja_templatetools.php on line 49
C# Numeric Data Type Conversion - The Scripts Library Community

Free Programming Tutorials & Source Code

 
  • Increase font size
  • Default font size
  • Decrease font size
Home C# C# Numeric Data Type Conversion

C# Numeric Data Type Conversion

(2 votes, average: 5.00 out of 5)
Type conversion is necessary in software development to assign value from value to another. Adding a string value to an int value is not possible without DataType conversion. In C#, DataType conversion is pretty easy, since lots of function are made just for data conversion. However, some operations between two different type do not required a DataType conversion, this is what we called it implicit conversion. What is implicit conversion? In simple words, implicit conversion takes place automatically without the need of type conversion, only when the DataType being assigned to is compatible with the other value being assigned to it. For example, an int value can be implicitly converted to a double, so a value of 2 is automatically converted to 2.00. But, the reverse is not true; int value cant hold a double value. Here is some example for implicit conversion:
Int vIntNumber = 2; Long vLongNumber = 2; Double vDoubleNumber = 3.14;

int can be implicitly converted to long
vLongNumber = vIntNumber; // this is true

A double can be assigned to int
vDoubleNumber = vIntNumber ; // this is true

A long may have a larger value then an int
vIntNumber = vLongNumber; // this is false

A double may be truncated if assigned to a long
vLongNumber = vDoudleNumber; //this is false

As you have seen in this example, an integer may not be able to handle double, or long number. Double and long can handle integer. Long cant handles a double number.
Explicit Casting
The automatic conversion, or implicit conversion, doesn’t work with all types. However, it’s necessary to perform a type conversion between two values of two incompatible DataType. It’s possible to hold a decimal value, after truncating the decimal part from it, into an int value. For Example: a decimal value of 3.14 can be held by an int value, after truncation the .14. So the value would be 3.
In this case, you should know that the decimal value would be truncated. Otherwise, the compiler will not show any error after applying the explicit type conversion. Here is why:
After applying a type conversion, a cast instruction tells the compiler that an explicit conversion between 2 values of 2 incompatible DataType will have place in the code. Thus, the compiler will consider this case as true, as the programmer knows that a truncated data will be lost.
The cast operator is added before the value being explicitly converted. The operator consists of the destination data type in parentheses ().
The following code samples show casts in action:
The cast operator is added before the value being converted. The data type converted to it should be between two parenthetic as the following
IntegerValue = (int)DoubleValue;

Here is few example of how type conversion works in C#:
int vIntNumber = 2; long vLongNumber = 2; double vDoubleNumber = 3.14; long vTooLong= 9999999999;

Explicitly convert long value to int value
vIntNumber = (int)vLongNumber;

Explicitly convert a value double to an int value. The fractional part is truncated.
vIntNumber = (int)vDoubleNumber; // value if vIntNumber after this statement is 3

Although accepted by the compiler, this generates an unpredictable result because the value is too large to be assigned to an int
vIntNumber = (int)vTooLong; // common programmers mistake