Swift Tutorial
Swift Data Types
Swift Control Flow
Swift Functions
Swift Collections
Swift OOPs
Swift Additional Topics
In Swift, numbers are represented using a variety of data types depending on their nature (whole numbers vs. numbers with fractional components) and the range of values they can hold. Here's a breakdown of integer and floating-point number types in Swift:
Integers are whole numbers that can be either positive, negative, or zero.
Int: This type represents a 32-bit or 64-bit integer depending on the platform's architecture. On a 32-bit platform, Int
is the same size as Int32
. On a 64-bit platform, it's the same size as Int64
.
var intValue: Int = 10
UInt: An unsigned integer type. Like Int
, its size depends on the platform's architecture (UInt32
on 32-bit platforms and UInt64
on 64-bit platforms).
var unsignedValue: UInt = 20
Fixed-size Integers: These are specific-sized integers that hold the same number of bits regardless of the platform:
Int8
: 8-bit signed integer.Int16
: 16-bit signed integer.Int32
: 32-bit signed integer.Int64
: 64-bit signed integer.UInt8
: 8-bit unsigned integer.UInt16
: 16-bit unsigned integer.UInt32
: 32-bit unsigned integer.UInt64
: 64-bit unsigned integer.Floating-point numbers are numbers that can have a fractional component.
Float: This type represents a 32-bit floating-point number and can represent a precision of at least 6 decimal digits.
var floatValue: Float = 3.14159
Double: Represents a 64-bit floating-point number and has a precision of at least 15 decimal digits. This is the preferred floating-point type because of its higher precision.
var doubleValue: Double = 3.141592653589793
If you need to work with whole numbers and aren't sure which type to choose, you can typically go with Int
. It adapts to the native word size of the platform and benefits from hardware optimization.
If you're dealing with fractional values, prefer Double
over Float
for most situations due to its higher precision.
If you're interfacing with APIs or libraries that specify particular sizes or types, or if you have specific, known data-size requirements, then use the fixed-size integers or the specific float type required.
Swift emphasizes type safety, and as a result, it doesn't allow implicit type conversions. If you need to convert between numeric types, you must explicitly create a new instance of the desired type.
Swift provides several data types for representing integers and floating-point numbers. For integers, you have types like Int8
, Int16
, Int32
, and Int64
representing signed integers, as well as their unsigned counterparts UInt8
, UInt16
, UInt32
, and UInt64
. For floating-point numbers, you have Float
and Double
.
You can declare and initialize integers in Swift using various data types. Here are examples:
// Signed Integers let intNumber: Int = 42 let int8Number: Int8 = 8 let int16Number: Int16 = 16 let int32Number: Int32 = 32 let int64Number: Int64 = 64 // Unsigned Integers let uintNumber: UInt = 42 let uint8Number: UInt8 = 8 let uint16Number: UInt16 = 16 let uint32Number: UInt32 = 32 let uint64Number: UInt64 = 64
You can perform various operations on integers in Swift, including addition, subtraction, multiplication, division, and modulo. Here's an example:
let a = 10 let b = 5 let sum = a + b let difference = a - b let product = a * b let quotient = a / b let remainder = a % b print("Sum: \(sum), Difference: \(difference), Product: \(product), Quotient: \(quotient), Remainder: \(remainder)")
Floating-point numbers in Swift include Float
and Double
. They can represent real numbers with decimal points. Here's an example:
let floatValue: Float = 3.14 let doubleValue: Double = 3.14159265359 print("Float value: \(floatValue), Double value: \(doubleValue)")
You can convert between integer and floating-point types using type casting. Here's an example:
let integerNumber = 42 let floatingPointNumber = Double(integerNumber) print("Integer to Double conversion: \(floatingPointNumber)")
NSNumber
is an Objective-C class that can be used in Swift to represent numbers in a type-safe way. NSDecimalNumber
is a subclass of NSNumber
that provides precise handling of decimal numbers. Here's an example:
import Foundation let nsNumber: NSNumber = 42.5 let nsDecimalNumber: NSDecimalNumber = NSDecimalNumber(string: "42.5") print("NSNumber value: \(nsNumber), NSDecimalNumber value: \(nsDecimalNumber)")
Swift provides functions for rounding and formatting numbers. Here's an example:
let valueToRound = 3.14159 let roundedValue = round(valueToRound) // Rounds to the nearest integer let formattedValue = String(format: "%.2f", valueToRound) // Formats to two decimal places print("Rounded value: \(roundedValue), Formatted value: \(formattedValue)")