Thursday, March 29, 2018

Swift 4 Introduction Series 1.4 - Swift Data Type Conversion

Data Type Conversion

We cannot perform computation for variables with different data type. For example, we cannot add a floating point number (Double) with a variable that is an integer (Int). To be able to perform such computation, we must convert the integer to a floating point number. Similarly, we cannot add a variable that belongs to datatype Int8 with a UInt16 variable.  


For conversion between data type the syntax is as follows:


let <constant_name> = <datatype>(<numeric_literals/constant/variables>)
Or
var <variable_name> = <datatype>(<numeric_literals/constant/variables>)
Or
<declared_variable_name> = <datatype_same_as_variable_name>(<numeric_literals/constant/variables>)


The basic rule for data type conversion is; the data to be converted is acceptable to the data type it is converted to.


Therefore, we must know the data type limit so that we can perform conversion without error.

Conversion between Bit Sized Integer

To convert between bit size data, we need to use the above syntax.


Example:


let numberA:UInt8 = 87
let numberB:Int16 = 43
let numberC = 23

let numberD = Int16(12)

let numberE = Int32(numberA)
var numberF = UInt16(numberB)
let numberG = UInt64(numberC)

numberF = UInt16(numberD)




As shown in the example above, we can include numeric literal in the conversion bracket as shown in NumberD. To reassign the variable, we need to match the datatype of the converter with the variable on the left hand side of the equation as shown in numberF in the last statement. For declaration, please note that the variable will take whatever data type that is specified by the converter.


We can also declare constant or variable first and perform assignment later. Please note that the converter must be of the same datatype as the declared variable or constant.


Example:


let numberI:UInt64

numberI = UInt64(numberA)

var numberJ:Int16

numberJ = Int16(numberC)




We can also perform computation as shown below:


let numberH = UInt32(numberA) + UInt32(numberB) + UInt32(numberC)




In the above example, we can ignore the converter if any of the number is the data type we want. See another example below.


In the following example, we are adding an Int8, Int16 and UInt32. If the datatype of the summation number we want is in the operand (in this case UInt32), then we can ignore the converter.


Example:


let numberX:Int8 = 54
let numberY:UInt32 = 885665
let numberZ:Int16 = 2565

let numberXYZ = UInt32(numberX) + numberY + UInt32(numberZ)


Important: Please note that during conversion, the assigned data type must be able to accept the converted number.


Example:


let numberK:UInt32 = 522

// The following statement will generate error since Int8 cannot accept any number larger than 128

let numberL = Int8(numberK)


Screen Shot 2017-11-03 at 22.55.40.png


More Example:


//: For data conversion use desiredDataType(InitialValueOrVariable)
let val36:UInt16 = 0xFFFE
let val37:UInt8 = 1
let val38:UInt16 = val36 + UInt16(val37)
//let val39 = val36 + 2 // This statement will generate error as it will exceed its maximum value
let val39 = Int(val36) + 2
// In the previous statement, we convert val36 from UInt16 to Int which allows more number.




Conversion between Integers

As discussed earlier, UInt and Int are derived from CPU capabilities. If the CPU is 32-bit, then UInt and Int is the same as UInt32 and Int32 respectively. Assuming our CPU is 64-bit, then we can use Int64 or Int interchangeably. However, it is advisable to stick to Int for interoperability.


To convert between UInt and Int, we can use the same syntax as mentioned before.


Example:


// Convert from Int to Uint
let numberM = 21546658785652

let numberN = UInt(numberM)




Example 2:


//Convert from UInt to Int
let numberO:UInt = 321654655

var numberP = Int(numberO)



Conversion between Floating Point

To convert between Float and Double, we use the same syntax.


Example:


// Convert from Float to Double
let shortDecimal1:Float = 2.658846542

let longDecimal1 = Double(shortDecimal1)




From the above example, we also notice that the decimal number is not exactly the same as the number originally input. This is due to the way computer works with floating point number. The last digit or 2 may not be accurate. In practice, we usually discard the last 2 decimal places in situation where decimal precision is important.


Example 2:


// Convert from Double to Float

let longDecimal2 = 9.32655646655879523

let shortDecimal2 = Float(longDecimal2)




To convert from long decimal places to short decimal places, usually the long form is being truncated.

Converting Floating Point and Integers

The following example demonstrates conversion from integer to floating point number (double).


Example:


// Convert from Int to Double

let number1 = 2546

let number2 = Double(number1)

print(number2)


If we look at the result on the right the display number is the same. If we use print statement, it will print the number with .0 since the constant is a double.


To convert from floating point number to integer, please note that the system will drop the decimals and keep the whole number. The system will not round up to the nearest whole number. It will just drop the decimal places.


Example:


// Convert from Double to Int

let number3 = 2.99464513

let number4 = Int(number3)

print(number4)




Data conversion during computation



Similarly, while perform computation and data type conversion, we do not need to convert the operand that has the data type we want.


Example:


let number10 = 6
let number11 = 0.654596

let number12 = Double(number10) + number11




More example:


// To perform computation with integer and floating point always convert the integer to floating point
let val40 = 2
let val41 = 0.1718
let val42 = Double(val40) + val41
// The previous statement will generate error if we do not convert val40 to Double as we cannot add Integer with Double

// The following statement produce no error because the datatype is infer during compilation time and the compiler will infer to the most appropriated type
let val43 = 2 + 0.1718




Conversion between String and Numbers

To convert from text to number, the text must be in proper number:


Example:


// Convert from String to Double / Int
let text1 = "12.345"

let number5 = Double(text1)

let number6 = Int(text1)

let text2 = "556"

let number7 = Int(text2)




As shown in the example above, the string "12.345" cannot be converted to Int.


Example:


// Convert from Double / Int to String
let number8 = 256

let text3 = String(number8)

let number9 = 2.78846562

var text4 = String(number9)




More example:


// To use numbers with string, we need to explicitly convert from a numeric data type to string
let myName = "Thomas"
let mySocialSecurity = 12532455
let myLine1 = myName + " Social Security number is " + String(mySocialSecurity)

// or we can string interpolation
let myline2 = "\(myName)'s Social Security number is \(mySocialSecurity)"

let hisName1 = "Steve"
let hisEarnings:Float = 145258
let myLine3 = "\(hisName1) just earned $\(hisEarnings * 0.25) for this project."



Comprehensive Type Casting

Type casting is also known as data type type conversion. The following example presents comprehensive type casting for basic data type:


//: Comprehensive Type Casting
// Convert from strings to integer
let str1 = "9384"
let num1 = Int(str1)
num1

// Convert from integer to string
let num2 = 5478955
let str2 = String(num2)
str2

// Convert from string to 32-bit integer
let str3 = "454234"
let num3 = Int32(str3)

// Convert from default integer to 16-bit integer
let numi1 = 54
let numi2 = Int16(numi1)

// Convert from default integer to 8-bit integer
let numi3 = 12
let numi4 = Int8(numi3)

// Convert from default 8-bit integer to default integer
let numi5 = numi4
let numi6 = Int(numi5)

// Convert from default double to float
let num4 = 25.3
let num5 = Float(num4)

// Convert from float to double
let num6 = num5
let num7 = Double(num6)

// Convert from default double to integer. Please note that the decimals will be trancated.
let num8 = 32.946
let num9 = Int(num8)

// Convert from integer to double. No change in value.
let num10 = 2145
let num11 = Double(num10)

// Convert from double to string
let num12 = 32.65
let str4 = String(num12)

// Convert from string (with decimals) to float and double. Please note that float can accept fewer decimals
let str5 = "2145.3256525656"
let num14 = Float(str5)
let num15 = Double(str5)



*****

No comments:

Post a Comment