Question: Most of the answers use Java (e.g. `String.format`) to get the job done, but I need a way to do this purely with Kotlin native to support multiplatform programming.

This means no using Java packages

Say a method like fun Float.toString(numOfDec: Int). I'd like the value to round e.g.:

`35.229938f.toString(1)` should return `35.2`

`35.899991f.toString(2)` should return `35.90`

Answer: 
I created the following Float extension (this should also work with Double):

```
/**
 * Return the float receiver as a string display with numOfDec after the decimal (rounded)
 * (e.g. 35.72 with numOfDec = 1 will be 35.7, 35.78 with numOfDec = 2 will be 35.80)
 *
 * @param numOfDec number of decimal places to show (receiver is rounded to that number)
 * @return the String representation of the receiver up to numOfDec decimal places
 */
fun Float.toString(numOfDec: Int): String {
    val integerDigits = this.toInt()
    val floatDigits = ((this - integerDigits) * 10f.pow(numOfDec)).roundToInt()
    return "${integerDigits}.${floatDigits}"
}

```
Use this in your scenario

```
 try {
            if (!TextUtils.isDigitsOnly(st)) {
                val rounded = Math.round(st.toFloat())
                val toHex = BigInteger(rounded.toString(), 10)
                val t = toHex.toString(16)
                t.toString()
            } else {
                val toHex = BigInteger(st, 10)
                val t = toHex.toString(16)
                t.toString()
            }
        } catch (e: NumberFormatException) {
            error
        }

```