Numbers
Convert numbers to/from strings
Section titled “Convert numbers to/from strings”Use String initializers for converting numbers into strings:
String(1635999) // returns "1635999"String(1635999, radix: 10) // returns "1635999"String(1635999, radix: 2) // returns "110001111011010011111"String(1635999, radix: 16) // returns "18f69f"String(1635999, radix: 16, uppercase: true) // returns "18F69F"String(1635999, radix: 17) // returns "129gf4"String(1635999, radix: 36) // returns "z2cf"Or use string interpolation for simple cases:
let x = 42, y = 9001"Between \(x) and \(y)" // equivalent to "Between 42 and 9001"Use initializers of numeric types to convert strings into numbers:
if let num = Int("42") { /* ... */ } // num is 42if let num = Int("Z2cF") { /* ... */ } // returns nil (not a number)if let num = Int("z2cf", radix: 36) { /* ... */ } // num is 1635999if let num = Int("Z2cF", radix: 36) { /* ... */ } // num is 1635999if let num = Int8("Z2cF", radix: 36) { /* ... */ } // returns nil (too large for Int8)Number types and literals
Section titled “Number types and literals”Swift’s built-in numeric types are:
- Word-sized (architecture-dependent) signed Int and unsigned UInt.
- Fixed-size signed integers Int8, Int16, Int32, Int64, and unsigned integers UInt8, UInt16, UInt32, UInt64.
- Floating-point types Float32/Float, Float64/Double, and Float80 (x86-only).
Literals
Section titled “Literals”A numeric literal’s type is inferred from context:
let x = 42 // x is Int by defaultlet y = 42.0 // y is Double by default
let z: UInt = 42 // z is UIntlet w: Float = -1 // w is Floatlet q = 100 as Int8 // q is Int8Underscores (_) may be used to separate digits in numeric literals. Leading zeros are ignored.
Floating point literals may be specified using significand and exponent parts (*«significand»* **e** *«exponent»* for decimal; **0x** *«significand»* **p** *«exponent»* for hexadecimal).
Integer literal syntax
Section titled “Integer literal syntax”let decimal = 10 // tenlet decimal = -1000 // negative one thousandlet decimal = -1_000 // equivalent to -1000let decimal = 42_42_42 // equivalent to 424242let decimal = 0755 // equivalent to 755, NOT 493 as in some other languageslet decimal = 0123456789
let hexadecimal = 0x10 // equivalent to 16let hexadecimal = 0x7FFFFFFFlet hexadecimal = 0xBadFacelet hexadecimal = 0x0123_4567_89ab_cdef
let octal = 0o10 // equivalent to 8let octal = 0o755 // equivalent to 493let octal = -0o0123_4567
let binary = -0b101010 // equivalent to -42let binary = 0b111_101_101 // equivalent to 0o755let binary = 0b1011_1010_1101 // equivalent to 0xB_A_DFloating-point literal syntax
Section titled “Floating-point literal syntax”let decimal = 0.0let decimal = -42.0123456789let decimal = 1_000.234_567_89
let decimal = 4.567e5 // equivalent to 4.567×10⁵, or 456_700.0let decimal = -2E-4 // equivalent to -2×10⁻⁴, or -0.0002let decimal = 1e+0 // equivalent to 1×10⁰, or 1.0
let hexadecimal = 0x1p0 // equivalent to 1×2⁰, or 1.0let hexadecimal = 0x1p-2 // equivalent to 1×2⁻², or 0.25let hexadecimal = 0xFEEDp+3 // equivalent to 65261×2³, or 522088.0let hexadecimal = 0x1234.5P4 // equivalent to 0x12345, or 74565.0let hexadecimal = 0x123.45P8 // equivalent to 0x12345, or 74565.0let hexadecimal = 0x12.345P12 // equivalent to 0x12345, or 74565.0let hexadecimal = 0x1.2345P16 // equivalent to 0x12345, or 74565.0let hexadecimal = 0x0.12345P20 // equivalent to 0x12345, or 74565.0Rounding
Section titled “Rounding”Rounds the value to the nearest whole number with x.5 rounding up (but note that -x.5 rounds down).
round(3.000) // 3round(3.001) // 3round(3.499) // 3round(3.500) // 4round(3.999) // 4
round(-3.000) // -3round(-3.001) // -3round(-3.499) // -3round(-3.500) // -4 *** careful here ***round(-3.999) // -4Rounds any number with a decimal value up to the next larger whole number.
ceil(3.000) // 3ceil(3.001) // 4ceil(3.999) // 4
ceil(-3.000) // -3ceil(-3.001) // -3ceil(-3.999) // -3Rounds any number with a decimal value down to the next smaller whole number.
floor(3.000) // 3floor(3.001) // 3floor(3.999) // 3
floor(-3.000) // -3floor(-3.001) // -4floor(-3.999) // -4Converts a Double to an Int, dropping any decimal value.
Int(3.000) // 3Int(3.001) // 3Int(3.999) // 3
Int(-3.000) // -3Int(-3.001) // -3Int(-3.999) // -3round,ceilandfloorhandle both 64 and 32 bit architecture.
Convert one numeric type to another
Section titled “Convert one numeric type to another”func doSomething1(value: Double) { /* ... */ }func doSomething2(value: UInt) { /* ... */ }
let x = 42 // x is an IntdoSomething1(Double(x)) // convert x to a DoubledoSomething2(UInt(x)) // convert x to a UIntInteger initializers produce a runtime error if the value overflows or underflows:
Int8(-129.0) // fatal error: floating point value cannot be converted to Int8 because it is less than Int8.minInt8(-129) // crash: EXC_BAD_INSTRUCTION / SIGILLInt8(-128) // okInt8(-2) // okInt8(17) // okInt8(127) // okInt8(128) // crash: EXC_BAD_INSTRUCTION / SIGILLInt8(128.0) // fatal error: floating point value cannot be converted to Int8 because it is greater than Int8.maxFloat-to-integer conversion rounds values towards zero:
Int(-2.2) // -2Int(-1.9) // -1Int(-0.1) // 0Int(1.0) // 1Int(1.2) // 1Int(1.9) // 1Int(2.0) // 2Integer-to-float conversion may be lossy:
Int(Float(1_000_000_000_000_000_000)) // 999999984306749440Random number generation
Section titled “Random number generation”arc4random_uniform(someNumber: UInt32) -> UInt32
This gives you random integers in the range 0 to someNumber - 1.
The maximum value for UInt32 is 4,294,967,295 (that is, 2^32 - 1).
Examples:
let flip = arc4random_uniform(2) // 0 or 1 let roll = arc4random_uniform(6) + 1 // 1...6 let day = arc4random_uniform(31) + 1 // 1...31 let year = 1990 + arc4random_uniform(10)General form:
let number = min + arc4random_uniform(max - min + 1)where number, max, and min are UInt32.
- There is a slight modulo bias with
arc4randomsoarc4random_uniformis preferred. - You can cast a
UInt32value to anIntbut just beware of going out of range.
Exponentiation
Section titled “Exponentiation”In Swift, we can exponentiate Doubles with the built-in pow() method:
pow(BASE, EXPONENT)In the code below, the base (5) is set to the power of the exponent (2) :
let number = pow(5.0, 2.0) // Equals 25