Initializer

init(exactly:)

Creates an integer from the given floating-point value, if it can be represented exactly.

Declaration

init?(exactly source: Double)

Parameters

source

A floating-point value to convert to an integer.

Discussion

If the value passed as source is not representable exactly, the result is nil. In the following example, the constant x is successfully created from a value of 21.0, while the attempt to initialize the constant y from 21.5 fails:

let x = Int(exactly: 21.0)
// x == Optional(21)
let y = Int(exactly: 21.5)
// y == nil

See Also

Converting Floating-Point Values

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.

init?<T>(exactly: T)

Creates an integer from the given floating-point value, if it can be represented exactly.

init(Double)

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

init(Float)

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

init(CGFloat)

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

init(Float80)

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

init?(exactly: Float)

Creates an integer from the given floating-point value, if it can be represented exactly.

init?(exactly: Float80)

Creates an integer from the given floating-point value, if it can be represented exactly.