This article explores different ways to convert an integer to a string in Kotlin. If the integer is negative, the sign should be preserved.

1. Using toString() function

The recommended solution is to use the toString() function that returns the string representation of the specified argument.

Download Code

2. Using String.format() function

Alternatively, you can use the String.format() function, which returns a formatted string by substituting the specified arguments.

Download Code

3. Using StringBuilder.append() function

Another plausible way to convert an integer to a string is using the append() function of the StringBuilder class, which is overloaded to accept data of any type.

Download Code

4. Using DecimalFormat.format() function

DecimalFormat class provides a variety of features to parse and format numbers. We can use it to format an integer to produce a string, as shown below:

Download Code

5. Using Plus Operator

Finally, you can concatenate the given integer with an empty string using the + operator or the plus() function to produce the string representation of the integer.

Download Code

That’s all about converting an integer to a string in Kotlin.