To convert a string to float in Kotlin, you can use the 'toFloat()' method of the String class.
In this example,
str1
with a value of "3.14"
.toFloat()
method and store the result in a variable float1
.float1
to standard output.fun main(args: Array<String>) {
val str1 = "3.14";
val float1 = str1.toFloat();
println("Float value is: $float1");
}
Float value is: 3.14
In this example,
str2
with a value of "10.5"
.toFloat()
method and store the result in a variable float2
.float2
to standard output.fun main(args: Array<String>) {
val str2 = "10.5";
val float2 = str2.toFloat();
println("Float value is: $float2");
}
Float value is: 10.5
In this tutorial, we learned How to convert String to Float in Kotlin language with well detailed examples.