Kotlin round()
Published Oct 27, 2023
Contribute to Docs
The round() function in Kotlin’s math package rounds a given number to the nearest integer. If the number is equidistant between two integers, it rounds to the integer that is even.
Syntax
round(a)
a: the number (eitherDoubleorFloat) to be rounded
The round() function returns the rounded number.
Example
This example uses the round() function to determine the rounded value of each number in a list:
import kotlin.math.roundfun main() {val numbers = listOf(5.4, 5.5, 5.6, 6.4, 6.5, 6.6)for (number in numbers) {val roundedNumber = round(number)println("$number rounded is: $roundedNumber")}}
Note: The numbers
5.5and6.5are equidistant between neighboring integers. Since 6 is even, theround()function returns6.0for both5.5and6.5.
The output of this code will be:
5.4 rounded is: 5.05.5 rounded is: 6.05.6 rounded is: 6.06.4 rounded is: 6.06.5 rounded is: 6.06.6 rounded is: 7.0
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours