Swift Tutorial Swift References

Swift - lround() Function



The Swift lround() function returns an integral value that is nearest to the argument value. In halfway cases, the argument is rounded away from zero.

Syntax

In Foundation framework, it is defined as follows:

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

Parameters

x Specify a value to round.

Return Value

Returns an integral value by rounding up the x to nearby integral value then casted to Int type.

Example:

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

import Foundation

print("lround(10.4) = \(lround(10.4))")
print("lround(10.6) = \(lround(10.6))")
print("lround(10.5) = \(lround(10.5))")
print("lround(11.5) = \(lround(11.5))")
print("lround(12.5) = \(lround(12.5))")
print("lround(-10.5) = \(lround(-10.5))")
print("lround(-11.5) = \(lround(-11.5))")
print("lround(-12.5) = \(lround(-12.5))")

The output of the above code will be:

lround(10.4) = 10
lround(10.6) = 11
lround(10.5) = 11
lround(11.5) = 12
lround(12.5) = 13
lround(-10.5) = -11
lround(-11.5) = -12
lround(-12.5) = -13

❮ Swift Math Functions