Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Swift Double rounded()
In this tutorial, we will learn about the Swift Double rounded() method with the help of examples.
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.
Did you find this article helpful?