How to convert Int to String in Kotlin
Kotlin doesn't automatically converts Integers to Strings. If you need to convert an Integer value to its String representation you will need to use one of the following options:
1. Use the .toString() method
This is the simplest and easiest way to convert an integer to a string in Kotlin. It is to use the native .toString method available from the standard library. Here's an example
fun main() {
var a : Int = 42
var aStr : String = a.toString()
print("Just converted an integer to string in Kotlin: ${aStr}")
}
Here's how the documentation describes the usage of the toString method
fun Byte.toString(radix: Int): String
Returns a string representation of this Byte value in the specified radix.
2. Use the String.format method
Another option is to format the string value using the String.format method. Here's an example on how it will look like
fun main() {
var a : Int = 42
var aStr : String = String.format("%d", a)
print("Just converted an integer to string in Kotlin: ${aStr}")
}
Here you can also see the language definition for this method
fun String.format(vararg args: Any?): String
Uses this string as a format string and returns a string obtained by substituting the specified arguments, using the default locale.