Format in Kotlin string templates

Unfortunately, there’s no built-in support for formatting in string templates yet, as a workaround, you can use something like:

"pi = ${pi.format(2)}"

the .format(n) function you’d need to define yourself as

fun Double.format(digits: Int) = "%.${digits}f".format(this)

This will work only in Kotlin/JVM.

There’s clearly a piece of functionality here that is missing from Kotlin at the moment, we’ll fix it.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)