Kotlin truncate()
Published Oct 27, 2023Updated May 15, 2024
Contribute to Docs
The truncate() method rounds a Double or Float argument to the next whole value towards zero. If the value is positive, it will be rounded down towards zero. If the value is negative, it will be rounded up towards zero.
Note:
truncate()does not return an integer value, rather it returns aDoubleorFloatthat equals an integer value.
Syntax
truncate(arg)
Where arg is a Double or Float number to be rounded.
Example
The following example demonstrates the behavior of truncate():
import kotlin.math.*//Imports the entire Kotlin Math libraryfun main() {println(truncate(2.3))println(truncate(0.54))println(truncate(-6.8))}
This outputs:
2.00.0-6.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