Data Type Conversions in Swift
Before we start let’s see what is type conversion. Type Conversion is used to check the type of instances to find out whether it belongs to a particular class type or not using the type conversion method or approach. Basically, a Type Conversion is a method in which we can convert one instance data type to another instance data type by casting the variable or value within braces. For Example: var stringToFloatNum: Float = Float(67891)!
In Swift, one data type is converted into another data type by an XCode compiler at the compiler time. The type conversions can only be applied to compatible data types like integer, string, float, double,…etc. In this article, we will be learning about the type conversions in a Swift programming language with some same examples converting a string data type to an integer data type, integer data type to string data type, integer data type to float data type, float data type to integer data type, float data type to string data type and string data type to a float data type.
Syntax:
var <variable_name>: <data_type> = <conver_data_type>(<value>)
1. Example for Converting a Number to String Data Type
var intNumberToString: String = String(intNumberValue)
2. Example for Converting a String to Float Data Type
var stringToFloatNum: Float = Float(decimalstringValue)!
Note: In type conversion, the destination data type cannot be smaller than the source data type.
Here is a few examples where Data Types in Swift can be used for Type Conversions.
- To Integer Data Type Conversion >> Int(parameter) or Int(value)
- To Float Data Type Conversion >> Float(parameter) or Float(value)
- To String Data Type Conversion >> String(parameter)! or String(value)!
Convert Integer to String
This is a swift program where we are accepting a variable value as an Integer data type and converting that Integer variable value to a string data type and displaying the value and type using a print statement.
Swift
// Swift program to convert integer to string import Swift // Initializing variable and assigning // an integer value to it. var intNumber: Int = 789 // Initializing variable and Converting integer // variable to string and assigning to it. var intNumberToString: String = String (intNumber) // Display output print ( "Integer Value = \(intNumber) of type \(type(of: intNumber))" ) print ( "String Value = \(intNumberToString) of type \(type(of: intNumberToString))" ) |
Output:
Integer Value = 789 of type Int String Value = 789 of type String
Convert String to Integer
This is a swift program where we accept a variable value as a string data type and convert that string variable value to an integer data type and display the value and type using a print statement.
Swift
// Swift program to convert string to integer import Swift // Initializing variable and // assigning string value to it. var string: String = "789" // Initializing variable and Converting // string variable to integer and // assigning to it. var stringToInt: Int = Int (string)! // Display the output print ( "String Value = \(string) of type \(type(of: string))" ) print ( "Integer Value = \(stringToInt) of type \(type(of: stringToInt))" ) |
Output:
String Value = 789 of type String Integer Value = 789 of type Int
Convert Integer to Float
This is a swift program where we are accepting a variable value as an integer data type and converting that integer variable value to a float data type and displaying the value and type using print statements.
Swift
// Swift program to convert integer to float import Swift // Initializing variable and assigning // integer value to it. var intNum: Int = 789 // Initializing variable and Converting integer // variable to float and assigning to it var intNumToFloat: Float = Float (intNum) // Display the output print ( "Integer Value = \(intNum) of type \(type(of: intNum))" ) print ( "Float Value = \(intNumToFloat) of type \(type(of: intNumToFloat))" ) |
Output:
Integer Value = 789 of type Int Float Value = 789.0 of type Float
Convert Float to Integer
This is a swift program where we are accepting a variable value as a float data type and converting that float variable value to an Integer data type and displaying the value and type using a print statement.
Swift
// Swift program to convert float to integer import Swift // Initializing variable and // assigning float value to it. var floatNum: Float = 789.0089 // Initializing variable and Converting float // variable to integer and assigning to it. var floatNumToInt: Int = Int (floatNum) // Display the output print ( "Float Value = \(floatNum) of type \(type(of: floatNum))" ) print ( "Integer Value = \(floatNumToInt) of type \(type(of: floatNumToInt))" ) |
Output:
Float Value = 789.0089 of type Float Integer Value = 789 of type Int
Convert String to Float
This is a swift program where we are accepting a variable value as a string data type and converting that string variable value to a float data type and displaying the value and type using a print statement.
Swift
// Swift program to convert String to Float import Swift // Initializing variable as string // and assigning value to it. var string : String = "789" // Initializing variable and Converting string // variable to float and assigning to it. var stringToFloatNum: Float = Float (string)! // Display the output print ( "String Value = \(string) of type \(type(of: string)) " ) print ( "Float Value = \(stringToFloatNum) of type \(type(of: stringToFloatNum))" ) |
Output:
String Value = 789 of type String Float Value = 789.0 of type Float
Contact Us