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 a Double or Float that 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 library
fun main() {
println(truncate(2.3))
println(truncate(0.54))
println(truncate(-6.8))
}

This outputs:

2.0
0.0
-6.0

All contributors

Looking to contribute?

Learn Kotlin on Codecademy