Swift Tutorial

Swift Data Types

Swift Control Flow

Swift Functions

Swift Collections

Swift OOPs

Swift Additional Topics

Swift - Integer, Floating-Point Numbers

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:

Integers are whole numbers that can be either positive, negative, or zero.

  1. 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
    
  2. 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
    
  3. 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:

Floating-point numbers are numbers that can have a fractional component.

  1. 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
    
  2. 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
    

Choosing the Right Type:

  1. 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.

  2. If you're dealing with fractional values, prefer Double over Float for most situations due to its higher precision.

  3. 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.

1. Data types for integers and floating-point in Swift:

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.

2. How to declare and initialize integers in Swift:

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

3. Operations on integers in Swift programming:

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)")

4. Working with floating-point precision in Swift:

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)")

5. Converting between integer and floating-point types in Swift:

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)")

6. Swift NSNumber and NSDecimalNumber for numbers:

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)")

7. Swift rounding and formatting for numbers:

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)")