Generic Initializer

init(_:)

Creates an integer from the given floating-point value, rounding toward zero.

Required. Default implementations provided.

Declaration

init<T>(_ source: T) where T : BinaryFloatingPoint

Parameters

source

A floating-point value to convert to an integer. source must be representable in this type after rounding toward zero.

Discussion

Any fractional part of the value passed as source is removed, rounding the value toward zero.

let x = Int(21.5)
// x == 21
let y = Int(-21.5)
// y == -21

If source is outside the bounds of this type after rounding toward zero, a runtime error may occur.

let z = UInt(-21.5)
// Error: ...the result would be less than UInt.min

Default Implementations

FixedWidthInteger Implementations

init?(String)

Creates a new integer value from the given string.

init<T>(T)

Creates an integer from the given floating-point value, rounding toward zero. Any fractional part of the value passed as source is removed.

SignedInteger Implementations

init<T>(T)

Creates a new instance from the given integer.

UnsignedInteger Implementations

init<T>(T)

Creates a new instance from the given integer.