The Math.truncate() function used to return the rounded value of the specified double and float value.
Syntax: fun truncate(x : DataType): DataType
Parameters: It can take data two data type such as Double and Float.
Returns: It returns the rounded value of the specified double and float value.
Code #1: Taking float data types as argument.
// Kotlin program to illustrate
// working of Math.truncate() method
import kotlin.math.truncate
fun main(args : Array<String>){
val float1 = 42.23f;
val float2 = 46.99f;
val float3 = 49.00f;
// Calling the tuncate() function
println(truncate(float1));
println(truncate(float2));
println(truncate(float3));
}
Output :
42.0 46.0 49.0
Code #2: Taking double data types as argument.
// Kotlin program to illustrate
// working of Math.truncate() method
import kotlin.math.truncate
fun main(args : Array<String>){
val Double1 = 12.23;
val Double2 = 22.99;
val Double3 = 34.00;
// Calling the tuncate() function
println(truncate(Double1));
println(truncate(Double2));
println(truncate(Double3));
}
Output :
12.0 22.0 34.0