The rounded()
method rounds the specified value to the closest int or long value and returns it. That is, 3.87 is rounded to 4 and 3.24 is rounded to 3.
Example
// round the value 3.87
var result = 3.87.rounded()
print(result)
// Output: 4.0
rounded() Syntax
The syntax of the rounded()
method is:
num.rounded()
Here, num
is a number.
rounded() Parameters
The rounded()
method doesn't take any parameters.
rounded() Return Values
The rounded()
method returns the nearest integral value.
Example: Swift Double rounded()
// round the value 1.878
var result1 = 1.878.rounded()
print(result1) // 2.0
// round the value 1.5
var result2 = 1.5.rounded()
print(result2) // 2.0
// round the value 1.34
var result3 = 1.34.rounded()
print(result3) // 1.0
Output
2.0 2.0 1.0
Here, the values are rounded to their nearest integral using the rounded()
method.