Swift Tutorial Swift References

Swift - lrint() Function



The Swift lrint() function returns an integral value by rounding up the specified number. If x falls exactly at the midway between two integers, the even integer is returned.

Syntax

In Foundation framework, it is defined as follows:

public func lrint(_ __x: Double) -> Int

Parameters

x Specify a value to round.

Return Value

Returns the value of x which is first rounded to nearby integral value then casted to Int type. If x falls exactly at the midway between two integers, the even integer is returned.

Example:

In the example below, lrint() function is used to round the given number.

import Foundation

print("lrint(10.4) = \(lrint(10.4))")
print("lrint(10.6) = \(lrint(10.6))")
print("lrint(10.5) = \(lrint(10.5))")
print("lrint(11.5) = \(lrint(11.5))")
print("lrint(12.5) = \(lrint(12.5))")

The output of the above code will be:

lrint(10.4) = 10
lrint(10.6) = 11
lrint(10.5) = 10
lrint(11.5) = 12
lrint(12.5) = 12

❮ Swift Math Functions