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.

Answer from Andrey Breslav on Stack Overflow
๐ŸŒ
Kotlin
kotlinlang.org โ€บ api โ€บ core โ€บ kotlin-stdlib โ€บ kotlin.text โ€บ format.html
format | Core API โ€“ Kotlin Programming Language
See java.util.Formatter class documentation for the syntax of format specifiers for the format string. 1.0 ยท import kotlin.test.* import java.util.* import java.util.regex.* fun main() { //sampleStart // format negative number in parentheses val negativeNumberInParentheses = "%(d means %1\$d".format(-31416) println(negativeNumberInParentheses) // (31416) means -31416 //sampleEnd } inline fun String.Companion.format(format: String, vararg args: Any?): String(source) Uses the provided format as a format string and returns a string obtained by substituting format specifiers in the format string with the provided arguments, using the default locale.
Discussions

Java String.format alternative in kotlin? - Kotlin Discussions
Hi, Iโ€™m using java String.format in my codes which i want to port to kotlin. For example String log = String.format(โ€œ%d, %s, o : ?โ€, value1, value2, value3, value4) How to convert above java code to kotlin? Thanks More on discuss.kotlinlang.org
๐ŸŒ discuss.kotlinlang.org
3
November 18, 2013
String formatting library for Kotlin (or Java) like in fmt/rust/python
Why not just use what the language gives you in its standard library? More on reddit.com
๐ŸŒ r/Kotlin
17
6
May 28, 2022
Kotlin String.format - Support - Kotlin Discussions
Hi all I am working on a Kotlin port of the Android Timber library for a quick introductory project. I have encountered an issue with (my use of?) the Kotlin standard lib. I am trying to use String.format with varargs. โ€ฆ More on discuss.kotlinlang.org
๐ŸŒ discuss.kotlinlang.org
1
April 4, 2016
๐Ÿ˜ต Your email address is not a string.
Unless you're doing something that involves parsing email addresses, you're only going to be using email addresses by passing them onto other systems that will do something more useful with them, and it's fine to keep them as strings, same as you do with other data types that have complex semantics that your application doesn't touch (postcodes/zipcodes, phone numbers, names, vehicle license/registration plates, social security numbers). Also Kotools types email validation is wrong. One reasonable approach to email validation is to fully implement the RFCs for email addresses. Another reasonable approach is to keep it simple and just check it contains the "@" sign. Kotools types does neither of those. It has an approach that will reject some well formed email addresses ("Bill Gates"@microsoft) whilst allowing other malformed ones (@@@.@). I can't see a good reason to choose this approach. More on reddit.com
๐ŸŒ r/Kotlin
23
0
August 12, 2024
๐ŸŒ
Kotlin
kotlinlang.org โ€บ docs โ€บ strings.html
Strings | Kotlin Documentation
February 4, 2026 - The String.format() function accepts a format string and one or more arguments. The format string contains one placeholder (indicated by %) for a given argument, followed by format specifiers.
๐ŸŒ
Programiz
programiz.com โ€บ kotlin-programming โ€บ string
Kotlin String and String Templates (With Examples)
Length of s1 string is 10. Strings s1 and s2 are equal. Third character is y. result = Hey there! How are you? Substring is "the" Visit Kotlin String class for more information on extension properties, extension, functions and constructors.
๐ŸŒ
Kotlin
kotlinlang.org โ€บ api โ€บ latest โ€บ jvm โ€บ stdlib โ€บ kotlin.text โ€บ format.html
format - Kotlin Programming Language
August 9, 2023 - See java.util.Formatter class documentation for the syntax of format specifiers for the format string. import java.util.Locale import kotlin.test.* fun main(args: Array<String>) { //sampleStart // format negative number in parentheses val negativeNumberInParentheses = String.format("%(d means %1\$d", -31416) println(negativeNumberInParentheses) // (31416) means -31416 //sampleEnd }
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ kotlin โ€บ format string in kotlin with examples
Format String in Kotlin With Examples - Spark By {Examples}
May 9, 2024 - How to format a string in Kothin? Formatting string is a standard operation in most programming languages, including Kotlin. In Kotlin, you can use the
๐ŸŒ
Jonas-rodehorst
jonas-rodehorst.dev โ€บ blog โ€บ koltin-string-formatting
Kotlin String Formatting with Examples
November 18, 2022 - The modern versions of Kotlin introduced another method for formatting strings that is even more intuitive and readable than the String.format() method. The new method for formatting strings is called String-Literals.
Find elsewhere
๐ŸŒ
Kotlin Discussions
discuss.kotlinlang.org โ€บ t โ€บ java-string-format-alternative-in-kotlin โ€บ 703
Java String.format alternative in kotlin? - Kotlin Discussions
November 18, 2013 - Hi, Iโ€™m using java String.format in my codes which i want to port to kotlin. For example String log = String.format(โ€œ%d, %s, o : ?โ€, value1, value2, value3, value4) How to convert above java code to kotlin? Thanโ€ฆ
๐ŸŒ
sebhastian
sebhastian.com โ€บ kotlin-string-format
Kotlin String format() method example | sebhastian
January 3, 2022 - Kotlin borrows the String.format() method from the Java language, so you can format your string values with it.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ kotlin โ€บ kotlin string format
How to Format String in Kotlin | Delft Stack
February 2, 2024 - This article shows two ways to format string in Kotlin namely, string templates i.e using $ (dollar) sign and using String.format() method.
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ mastering-kotlin-string-formatting-what-you-need-to-know
Mastering Kotlin String Formatting: A Beginner's Guide
September 27, 2024 - Kotlin, being one of the modern programming languages, provides a flexible and powerful approach to string formatting through string templates, the String.format() method, and other utilities available in the String class.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ kotlin โ€บ kotlin numbers โ€บ number formatting in kotlin
Number Formatting in Kotlin | Baeldung on Kotlin
March 26, 2025 - Javaโ€™s String.format method is convenient for formatting an input using the printf format string. We can easily build a method in Kotlin that invokes the Java method to convert a Kotlin Double number to a String:
๐ŸŒ
Stonesoupprogramming
stonesoupprogramming.com โ€บ 2017 โ€บ 11 โ€บ 17 โ€บ kotlin-string-formatting
Kotlin String Formatting โ€“ Stone Soup Programming
November 6, 2017 - Using Kotlin String templates Bob is a good chef Bob is a great chef %[flags][width][.precision]type Formatting Symbols %b Boolean %c Character %d Signed Integer %e Float in scientific format %f Float in decimal format %g Float in either decimal or scientific notation based on value %h Hashcode of argument %n Line separator %o Octal Integer %s String %t Date or Time %x Hexadecimal Integer
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ kotlin โ€บ kotlin strings โ€บ kotlin string templates
Kotlin String Templates | Baeldung on Kotlin
March 19, 2024 - Kotlin has enriched the Java String class with additional functionality. For example, the method padEnd() allows us to format a String, so that the expression:
๐ŸŒ
Hyperskill
hyperskill.org โ€บ learn โ€บ step โ€บ 21438
String formatting
Hyperskill is an educational platform for learning programming and software development through project-based courses, that helps you secure a job in tech. Master Python, Java, Kotlin, and more with real-world coding challenges.
๐ŸŒ
Kotlin
kotlinlang.org โ€บ docs โ€บ java-to-kotlin-idioms-strings.html
Strings in Java and Kotlin | Kotlin Documentation
November 13, 2025 - // Java String result = """ Kotlin Java """; System.out.println(result); ... If you put the triple-quote on the same line as the last word, this difference in behavior disappears. In Kotlin, you can format your line with the quotes on the new line, and there will be no extra empty line in the output.
๐ŸŒ
Reddit
reddit.com โ€บ r/kotlin โ€บ string formatting library for kotlin (or java) like in fmt/rust/python
r/Kotlin on Reddit: String formatting library for Kotlin (or Java) like in fmt/rust/python
May 28, 2022 -

I'm trying to find a string formatting library for Kotlin, or Java, that uses similar specifiers like it's used in C++, Python or Rust. What I mean by that is shown on these examples:

// C++
std::cout << fmt::format("Hello 0x{:08X}\n", 123);

This will print Hello 0x0000007B.

# Python
print("Hello {:.3}".format(1.0153))

This will print Hello 1.02.

I'd like this lib mostly for consistency with my current sources in other languages. Does anyone know if such library exists? (I've tried to find one, but without any luck)

๐ŸŒ
GitHub
github.com โ€บ Simplx-dev โ€บ kotlin-format
GitHub - Simplx-dev/kotlin-format: A drop-in replacement for Java String.format() in Kotlin (JVM, Native, JS)
A drop-in replacement for Java String.format() in Kotlin (JVM, Native, JS) - Simplx-dev/kotlin-format
Starred by 21 users
Forked by 2 users
Languages ย  Kotlin 100.0% | Kotlin 100.0%
๐ŸŒ
Kotlin Discussions
discuss.kotlinlang.org โ€บ support
Kotlin String.format - Support - Kotlin Discussions
April 4, 2016 - Hi all I am working on a Kotlin port of the Android Timber library for a quick introductory project. I have encountered an issue with (my use of?) the Kotlin standard lib. I am trying to use String.format with varargs. internal fun prepareLog(priority: Int, t: Throwable?, message: String, vararg args: Any?)